1. 程式人生 > >java連接mysql數據庫8.0以上版本過程中遇到的坑

java連接mysql數據庫8.0以上版本過程中遇到的坑

per host ive may req 驅動 nav via forname

來自:https://blog.csdn.net/u013276277/article/details/80255468

首先,我居然不能用navicat客戶端連接上mysql8.0數據庫報1251錯誤,這個的解決方式已經在我的上一篇博客中解決了。然後我又遇到了java無法連接mysql數據庫8.0的問題。

報錯:Loading class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver‘. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

Wed May 09 16:25:23 CST 2018 WARN: Establishing SSL connection without server‘s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn‘t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false‘. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

上面這段報錯中加粗的文字很重要,她告訴你mysql8.0和之前版本的區別,首先驅動換了,不是com.mysql.jdbc.Driver而是‘com.mysql.cj.jdbc.Driver‘,此外mysql8.0是不需要建立ssl連接的,你需要顯示關閉。最後你需要設置CST。所以我們需要在之前我們熟悉的java連接數據庫代碼的基礎之上改動以下兩行代碼:

            Class.forName("com.mysql.cj.jdbc.Driver");
//          Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?useSSL=false&serverTimezone=UTC","root","password");
// conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo","root","password");

改動完了之後你就可以成功連上mysql數據庫數據庫8.0了。

java連接mysql數據庫8.0以上版本過程中遇到的坑