jdbc连接MysQL数据库时报错:The server time zone value ‘ ‘ is unrecognized or represents more than

报错: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中加入时区

 完整URL即:

String url = "jdbc:mysql://localhost:3306/test_db?characterEncoding=utf-8&serverTimezone=Asia/Shanghai"

中国是GMT+8时区(东八区),所以在url后面加上:serverTimezone=Asia/Shanghai

或 serverTimezone=Asia/Hongkong 或 serverTimezone=GMT%2B8(注:%2B是“+”号)

注意:Asia/Shanghai 、Asia/Hongkong,这个主要看所在服务器的操作系统,如龙芯电脑的时区存储中,Asia中就缺少Shanghai。


方法二:修改MySQL的time_zone

需要改变数据库的配置:命令行登陆mysql,修改time_zone变量的值。

set global time_zone ='+8:00';

方法三:降低jdbc驱动的版本

降低jdbc的驱动,使用更低版本的MySQL jdbc驱动。

分析:

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.11</version>
</dependency>

mysql-jdbc 6.0以上的版本在连接数据库时,需要在url后面指定时区。

猜你喜欢

转载自blog.csdn.net/qq_37738899/article/details/122162444