最近在看書學些新東西,用的DB是MySQL,一如既往到mvnrepository找dependency貼到pom.xml中,這次想說用較新的版本看看,就拿了6.0.6版本的,裝好並設定好mybatis的資料庫連線以後一執行…

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.

趕緊換回以前常用的5.1.39版看看,可以正常執行,看來是新版本的Driver做了什麼改變,順著訊息的描述將設定檔的driver名稱改為com.mysql.cj.jdbc.Driver,一執行又出現新的錯誤訊息

java.sql.SQLException: The server time zone value '�x�_�зǮɶ�' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

沒看錯,真的就是'�x�_�зǮɶ�',看起來是無法辨識時區,這次修改jdbc url,加上serverTimezone的相關設定(CST是中原標準時間)

jdbc:mysql://localhost:3306/myDB?serverTimezone=CST

再執行就正常了,不過還有個從我用5.1.39版就會出現的提示訊息,是跟SSL相關的

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.

如果沒有要用SSL的話,在jdbc url後面加上useSSL=false的設定即可

jdbc:mysql://localhost:3306/myDB?serverTimezone=CST&useSSL=false

注意:如果是在XML設定檔中設定,需將&換成&,否則會有例外錯誤

實體 "useSSL" 的參照結尾必須為 ';' 分界字元。

參考資料:com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver的区别 serverTimezone设定