基於上一篇文章JDBC-idea導入mysql鏈接java的jar包, jar包已經成功導入,接下來就是鏈接數據庫java
import java.sql.Connection; import java.sql.DriverManager; public class SqlConnection { public static void main(String[] args) throws Exception{ // 1.註冊驅動 Class.forName("com.mysql.cj.jdbc.Driver"); // 2.獲取鏈接 url, 用戶名, 密碼 //"jdbc:mysql://localhost:3306/grasg?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; String DATABASE = "grasg"; String USER = "root"; String PASSWORD = "316426"; String url = "jdbc:mysql://localhost:3306/" + DATABASE + "?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; Connection con = DriverManager.getConnection(url,USER,PASSWORD); System.out.println(con); } }
說明mysql
com.mysql.jdbc.Driver
變動爲com.mysql.cj.jdbc.Driver