在使用JDBC連接mysql資料庫的時候發生SQLException '¤¤°ê¼Ð·Ç®É¶¡'的異常情況! ...
相信很多小伙伴和我一樣遇到了這類問題,在使用JDBC連接mysql資料庫的時候發生SQLException如下所示的異常情況!
java.sql.SQLException: 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 specifc time zone value if you want to utilize time zone support. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.ast.service.impl.VboxDataServiceImpl.insertAll(VboxDataServiceImpl.java:34) at com.ast.controller.Thread_send.run(Thread_send.java:85) 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 specifc time zone value if you want to utilize time zone support. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85) at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132) at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2241) at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2265) at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319) at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825) ... 7 more
這其實是由於在安裝的時候沒有選擇使用位置,mysql預設使用美國時間,所以得改成我們東八區的時間。
所以應該怎麼做呢?
首先網上有很多方法是更改資料庫中的 time_zone,如下圖:
這個方法只能是解決這一次問題,重啟之後依然會報錯,還需要再次更改。
最好的方法就是在jdbc配置中添加?serverTimezone=UTC到末尾。
如下圖所示: