MySQL, JDBC, Eclipse
下載 MySQL JDBC產品 Connector/J
自行解壓到一個自訂目錄
設定Eclipse,匯入此JDBC JAR
Project/Properties
Libraries/Add External JAR 選擇Connector/J所在的目錄,找到名為
測試程式碼
下載 MySQL JDBC產品 Connector/J
自行解壓到一個自訂目錄
設定Eclipse,匯入此JDBC JAR
Project/Properties
Libraries/Add External JAR 選擇Connector/J所在的目錄,找到名為
mysql-connector-java-[version]
-bin.jar 的JAR檔
測試程式碼
import java.sql.*;
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
留言