driver=com.mysql.cj.jdbc.Driver(mysql8.x)
url=jdbc:mysql://localhost:3306/mytest?useSSL=false&serverTimezone=UTC
username=root
password=yourpassword
8以上的driver 和url和之前的不同
單元測試代碼
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtlTool {
public static Connection getConnection() {
//獲取mySql數(shù)據(jù)庫的驅(qū)動類
String driver="com.mysql.cj.jdbc.Driver";
//連接數(shù)據(jù)庫
String url="jdbc:mysql://localhost:3306/mytest?useSSL=false&serverTimezone=UTC";
//連接mySql的用戶名
String name="root";
//連接mySql的密碼
String password="yourpassword";
try {
Class.forName(driver);
Connection conn=DriverManager.getConnection(url, name, password);
System.out.println("成功連接數(shù)據(jù)庫");
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("加載驅(qū)動程序有錯誤");
return null;
} catch(SQLException e) {
e.printStackTrace();
System.out.println("取得連接時有錯誤,核對用戶名和密碼");
return null;
}
}
public static void main(String[] args) throws SQLException{
Connection cc=JDBCUtlTool.getConnection();
System.out.println(cc);
}
}