mysql-8.0.11 jdbc连接数据库 The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more… 错误

Exception in thread “main” 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.

解决方法

根据异常提示可知需要添加对应的时区,修改url即可

String url = String.format(“jdbc:mysql://%s:%d/%s?characterEncoding=%s&serverTimezone=UTC”, host, port, database, encoding);


在XML中

提示系统时区出现错误,可以在mysql中执行命令: 

set global time_zone='+8:00' 
或者在数据库驱动的url后加上serverTimezone=UTC参数

写代码的时候要注意,如果该参数是‘?’后的第一个,即

<property name="jdbcUrl"> jdbc:mysql://localhost:3306/exam?serverTimezone=UTC </property>

是没有问题的,但如果不是第一个,即

<property name="jdbcUrl"> jdbc:mysql://localhost:3306/exam?characterEncoding=utf8&serverTimezone=UTC </property>

这种写法是会报错的,会提示The reference to entity “serverTimezone” must end with the ‘;’ delimiter. 
运行后控制台也会出现 
对实体 “serverTimezone” 的引用必须以 ‘;’ 分隔符结尾。 
的错误提示。 
将代码改为

<property name="jdbcUrl"> jdbc:mysql://localhost:3306/exam?characterEncoding=utf8&amp;serverTimezone=UTC </property>

即可。在xml的配置文件中 ;要用 &amp; 代替。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值