異常錯誤:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC dri ...
異常錯誤: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.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Thu Sep 27 00:18:47 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. 2 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. 3 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) 4 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) 5 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) 6 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) 7 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) 8 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) 9 at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:832) 10 at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) 11 at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) 12 at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207) 13 at java.sql.DriverManager.getConnection(DriverManager.java:664) 14 at java.sql.DriverManager.getConnection(DriverManager.java:247) 15 at com.jdbc.chap02.sec03.Demo1.main(Demo1.java:33) 16 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. 17 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 18 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 19 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 20 at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 21 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) 22 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85) 23 at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128) 24 at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2236) 25 at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2260) 26 at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1314) 27 at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:963) 28 at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:822) 29 ... 6 moreView Code
我用了8.0版本的MySQL連接驅動。現在按照最新官方提示支持將com.mysql.jdbc.Driver 改為 com.mysql.cj.jdbc.Driver
1 //驅動名稱 2 private static String jdbcName = "com.mysql.cj.jdbc.Driver"; 3 //資料庫地址 4 private static String dbUrl = "jdbc:mysql://localhost:3306/db_book"; 5 //資料庫用戶名 6 private static String dbUser = "root"; 7 //資料庫密碼 8 private static String dbPassword = "123456";
我們還需要在訪問資料庫的Url後面加上以下的語句即可:
1 private static String dbUrl = "jdbc:mysql://localhost:3306/db_book?serverTimezone=GMT%2B8"
從錯誤上看應該是時區的錯誤,所以我們只需要設置完畢系統的時區即可。這裡的GMT%2B8
代表東八區。
還有一種解決辦法就是設置整個資料庫的時區,可以執行下麵的語句來完成:
1 show variables like '%time_zone%' 2 set global time_zone='+8:00'; 3 #執行結果: 4 Variable_name Value 5 system_time_zone 6 time_zone SYSTEM