使用高版本mysql-connector-java的时区问题

错误信息

今天在复习 JavaEE 相关内容的时候,遇到报错

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.

然后发现他的driver也改成com.mysql.cj.jdbc.Driver

原因

​ 驱动包版本:mysql-connector-java-8.0.18.jar

​ 后来在查资料后发现数据库安装时默认为英语,是0:00时区,但是用以前旧版本的MySql jdbc 驱动都不会让你设置时区,新版就需要了。。。。

解决方法

将连接数据库的url加上,UTC(世界均衡时间)参数,或者GMT(格林尼治时间),如下:

jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=UTC
jdbc:mysql://localhost:3306/demo?serverTimezone=GMT
jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=GMT

不过后来测试发现,这个时间比北京时间早8个小时,所以使用北京时间如下:

jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8
jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=GMT%2B8

更多内容大家可以访问我的个人博客:一只大大怪

posted @ 2020-03-26 12:41  一只大大怪  阅读(1928)  评论(0编辑  收藏  举报