使用8.0版本jdbc驅動連接數據庫操作


首先,我居然不能用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連接數據庫代碼的基礎之上改動以下兩行代碼:

[java] 
  1. Class.forName("com.mysql.cj.jdbc.Driver");  
  2. Class.forName("com.mysql.jdbc.Driver");  
  3. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo?useSSL=false&serverTimezone=UTC","root","password");  
  4.   conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_demo","root","password");  
            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了。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com