0%

解决java.sql.SQLException: The server time zone value is unrecognized or represents more than one time zone

我使用了 mysql-connector-java-8.0.19,运行程序时报错:

1
2
3
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.

按错误提示,需要在 spring.datasource.url 添加 serverTimezone=Asia/Shanghai 参数或者 GMT%2b8 表示 GMT+8 ,有些网友说写 CTT,我不知道写台湾的时区有啥意义。

1
2
3
4
spring:
# jdbc配置
datasource:
url: jdbc:mysql://localhost:3306/db?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false

如果mysql-connector-java用的6.0以上的,需要把com.mysql.jdbc.Driver 改为com.mysql.cj.jdbc.Driver,不然每次启动项目,都会有红色提示。