使用MySQL报错com.mysql.cj.jdbc.Driver和The server time zone value 'xxx' is unrecognized

使用MySQL生成的错误日志如下:

F:\java\JDK\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar=12164:F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\bin" -Dfile.encoding=UTF-8 -classpath "F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar;F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\plugins\junit\lib\junit-rt.jar;F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\plugins\junit\lib\junit5-rt.jar;F:\java\JDK\jre\lib\charsets.jar;F:\java\JDK\jre\lib\deploy.jar;F:\java\JDK\jre\lib\ext\access-bridge-32.jar;F:\java\JDK\jre\lib\ext\cldrdata.jar;F:\java\JDK\jre\lib\ext\dnsns.jar;F:\java\JDK\jre\lib\ext\jaccess.jar;F:\java\JDK\jre\lib\ext\jfxrt.jar;F:\java\JDK\jre\lib\ext\localedata.jar;F:\java\JDK\jre\lib\ext\nashorn.jar;F:\java\JDK\jre\lib\ext\servlet-api.jar;F:\java\JDK\jre\lib\ext\sunec.jar;F:\java\JDK\jre\lib\ext\sunjce_provider.jar;F:\java\JDK\jre\lib\ext\sunmscapi.jar;F:\java\JDK\jre\lib\ext\sunpkcs11.jar;F:\java\JDK\jre\lib\ext\zipfs.jar;F:\java\JDK\jre\lib\javaws.jar;F:\java\JDK\jre\lib\jce.jar;F:\java\JDK\jre\lib\jfr.jar;F:\java\JDK\jre\lib\jfxswt.jar;F:\java\JDK\jre\lib\jsse.jar;F:\java\JDK\jre\lib\management-agent.jar;F:\java\JDK\jre\lib\plugin.jar;F:\java\JDK\jre\lib\resources.jar;F:\java\JDK\jre\lib\rt.jar;E:\预IDEA项目\文章管理\target\test-classes;E:\预IDEA项目\文章管理\target\classes;C:\Users\Administrator\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Administrator\.m2\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar;C:\Users\Administrator\.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.mycom.myapp.essayManagement.dao.EssayDaoTest,testSelectAll
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.
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:827)
	at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:447)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at com.mycom.myapp.essayManagement.utils.JDBCUtils.getConnection(JDBCUtils.java:30)
	at com.mycom.myapp.essayManagement.dao.EssayDao.selectAll(EssayDao.java:24)
	at com.mycom.myapp.essayManagement.dao.EssayDaoTest.testSelectAll(EssayDaoTest.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	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:2139)
	at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2163)
	at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1301)
	at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:958)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:817)
	... 30 more

java.lang.NullPointerException
	at com.mycom.myapp.essayManagement.dao.EssayDao.selectAll(EssayDao.java:28)
	at com.mycom.myapp.essayManagement.dao.EssayDaoTest.testSelectAll(EssayDaoTest.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code -1

经过一番研究发现,我使用的mysql-connector-java.jar包版本是8.0.17

使用MySQL报错com.mysql.cj.jdbc.Driver和The server time zone value 'xxx' is unrecognized_第1张图片

而我使用的数据库连接驱动却是com.mysql.jdbc.Driver

解决方法有两种:

第一种:改变数据库驱动连接和url

# 数据库驱动
jdbc.driver=com.mysql.cj.jdbc.Driver
# 数据库url
jdbc.url=jdbc:mysql://localhost:3306/essayManagement?serverTimezone=GMT%2B8

如果只将com.mysql.jdbc.Driver改成com.mysql.cj.jdbc.Driver,那么就会报如下错:

F:\java\JDK\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar=12614:F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\bin" -Dfile.encoding=UTF-8 -classpath "F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\lib\idea_rt.jar;F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\plugins\junit\lib\junit-rt.jar;F:\IDEA\InstallationFiles\IntelliJ IDEA 2018.3.5\plugins\junit\lib\junit5-rt.jar;F:\java\JDK\jre\lib\charsets.jar;F:\java\JDK\jre\lib\deploy.jar;F:\java\JDK\jre\lib\ext\access-bridge-32.jar;F:\java\JDK\jre\lib\ext\cldrdata.jar;F:\java\JDK\jre\lib\ext\dnsns.jar;F:\java\JDK\jre\lib\ext\jaccess.jar;F:\java\JDK\jre\lib\ext\jfxrt.jar;F:\java\JDK\jre\lib\ext\localedata.jar;F:\java\JDK\jre\lib\ext\nashorn.jar;F:\java\JDK\jre\lib\ext\servlet-api.jar;F:\java\JDK\jre\lib\ext\sunec.jar;F:\java\JDK\jre\lib\ext\sunjce_provider.jar;F:\java\JDK\jre\lib\ext\sunmscapi.jar;F:\java\JDK\jre\lib\ext\sunpkcs11.jar;F:\java\JDK\jre\lib\ext\zipfs.jar;F:\java\JDK\jre\lib\javaws.jar;F:\java\JDK\jre\lib\jce.jar;F:\java\JDK\jre\lib\jfr.jar;F:\java\JDK\jre\lib\jfxswt.jar;F:\java\JDK\jre\lib\jsse.jar;F:\java\JDK\jre\lib\management-agent.jar;F:\java\JDK\jre\lib\plugin.jar;F:\java\JDK\jre\lib\resources.jar;F:\java\JDK\jre\lib\rt.jar;E:\预IDEA项目\文章管理\target\test-classes;E:\预IDEA项目\文章管理\target\classes;C:\Users\Administrator\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Administrator\.m2\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar;C:\Users\Administrator\.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.mycom.myapp.essayManagement.dao.EssayDaoTest,testSelectAll
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:827)
	at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:447)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at com.mycom.myapp.essayManagement.utils.JDBCUtils.getConnection(JDBCUtils.java:30)
	at com.mycom.myapp.essayManagement.dao.EssayDao.selectAll(EssayDao.java:24)
	at com.mycom.myapp.essayManagement.dao.EssayDaoTest.testSelectAll(EssayDaoTest.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	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:2139)
	at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2163)
	at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1301)
	at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:958)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:817)
	... 30 more

java.lang.NullPointerException
	at com.mycom.myapp.essayManagement.dao.EssayDao.selectAll(EssayDao.java:28)
	at com.mycom.myapp.essayManagement.dao.EssayDaoTest.testSelectAll(EssayDaoTest.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code -1

所以同时要在url后添加?serverTimezone=GMT%2B8

使用MySQL报错com.mysql.cj.jdbc.Driver和The server time zone value 'xxx' is unrecognized_第2张图片

第二种解决方法

那么就降低mysql-connector-java.jar包的版本,在这里使用5.1.38版本。

使用MySQL报错com.mysql.cj.jdbc.Driver和The server time zone value 'xxx' is unrecognized_第3张图片

同时连接数据库的配置仍是使用com.mysql.jdbc.Driver

使用MySQL报错com.mysql.cj.jdbc.Driver和The server time zone value 'xxx' is unrecognized_第4张图片

这样就会成功,不会报错。

你可能感兴趣的:(异常处理)