Eclipse新建專案,連線資料庫
阿新 • • 發佈:2019-01-27
下載MySQL資料庫connector-java
匯入mysql驅動到專案中
右鍵–>build path
選擇Add External JARS,再新增剛下載好的connector
新增程式碼測試連線能否成功
import java.sql.*;
public class test {
public static void main(String args[])
{
//載入JdBC驅動
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("load Driver successfully" );
} catch (Exception e) {
System.out.println("load Driver error");
e.printStackTrace();
}
//連線資料庫
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","liangxi333");
//jdbc:mysql://localhost:埠號/資料庫名稱","使用者名稱","密碼"
System.out.println("connect sql successfully");
} catch (Exception e) {
System.out.println("connect sql erro");
e.printStackTrace();
}
}
}
測試成功!!!!!