【BUG】java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or r

控制台报错信息:

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: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:207)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.liao.shiro2_database.Dao.getConnection(Dao.java:22)
    at com.liao.shiro2_database.Dao.listRoles(Dao.java:45)
    at com.liao.shiro2_database.Dao.main(Dao.java:79)
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:2234)
    at com.MysqL.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2258)
    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)
    ... 8 more

出现此错误的原因是数据库时区的问题,解决方法有2种。

第一种,访问数据库的URL 后面加上时区。

jdbc:MysqL://127.0.0.1:3306/shiro?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8

此处GMT%2B8代表东八区

第二种:修改MysqL数据库的时区

输入:show variables like '%time_zone%';

输入: set global time_zone='+8:00';

相关文章

连接数据库的方式:第一种方式:ODBC:开放数据库连接是微软...
JDBCRequest 使用VariableNamesmysql:数据库连接池对象var...
 1.JDBCDBC(JavaDataBaseConnectivity):Java数据库连接技术...
1.需要jar包的支持:java.sqljavax.sqlmysql-conneter-java....
1.简介Activiti是一个业务流程管理(BPM)框架,它是覆盖了业务...
1.JDBC体系系统一组规范:接口JDBC接口(API)包括两个层次:...