Java連線MySQL資料庫8.0以上版本遇到的坑(The new driver class is `com.mysql.cj.jdbc.Driver)
阿新 • • 發佈:2018-12-16
我遇到了java無法連線mysql資料庫8.0.1的問題。
報錯: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連線資料庫程式碼的基礎之上改動以下兩行程式碼:
載入驅動類
// static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false";
String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
建立連線
// String path = "com.mysql.jdbc.Driver";
String path = "com.mysql.cj.jdbc.Driver";