1. 程式人生 > 實用技巧 >關於mysql-connector-java高版本出現的問題

關於mysql-connector-java高版本出現的問題

框架:spring-boot+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.

進入列表頁面:

Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' 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 specific time zone value if you want to utilize time zone support.

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_131]

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_131]

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]

at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]

解決方法①:

更改propertie.yml中的mysql的
driver-class-name: com.mysql.cj.jdbc.Driver

重新啟動進入列表頁面:依舊報錯

Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'й���׼ʱ��' 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 specific time zone value if you want to utilize time zone support.
 at sun.reflect.GeneratedConstructorAccessor29.newInstance(Unknown Source) ~[na:na]
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_131]
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_131]

解決方法②:

更改propertie.yml中的mysql的
 url: jdbc:mysql://localhost:3306/maven?serverTimezone=UTC
執行正常!

一勞永逸解決方法:

  新增版本號即可


問題描述:

這是由於資料庫和系統時區差異所造成的,沒有指定MySQL驅動版本的情況下它自動依賴的驅動是8.0.22的的版本
此問題為時區問題,在 JDBC 的連線 url 部分加上 serverTimezone=UTC 即可。
如果選擇utc會比中國時間早8個小時,如果在中國,可以選擇Asia/Shanghai或者Asia/Hongkong

easypeasy!