Spring Boot 操作数据库出现 "You must configure either the server or JDBC driver" 的解决方案。

Spring Boot 配置数据库是出现了以下的异常:

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.

产生原因分析:

1)查看数据库的版本:查看pom文件


	mysql
	mysql-connector-java
	runtime

2)点击  mysql-connector-java 继续查看版本


   mysql
   mysql-connector-java
   ${mysql.version}
   
     
        protobuf-java
        com.google.protobuf
     
  

3)点击 ${mysql.version} 查看版本

8.0.18

问题产生原因:

这是由于mysql 6.0.2 版本以上会出现 数据库和系统时区差异

解决方案一:更改mysql版本



    mysql
    mysql-connector-java
    5.1.48

注意在添加这个版本的时候需要更改yml文件中的信息:

driver-class-name: com.mysql.jdbc.Driver

解决方式二:添加时区

下面在pom文件中使用默认的mysql驱动。


	mysql
	mysql-connector-java
	runtime

在application.yml 文件中添加

1)在 6.0.x 版本以上都需要添加。

driver-class-name: com.mysql.cj.jdbc.Driver

2)在自己所要查询的数据中添加:

url: jdbc:mysql://localhost:3306/springdb?serverTimezone=UTC

  注意:但是添加这个之后,会出现添加数据时依然存在时区问题,所以需要更改成为以下代码

url: jdbc:mysql://localhost:3306/springdb?serverTimezone=Asia/Shanghai

 

你可能感兴趣的:(Spring,Boot)