org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘helloController‘: Unsatisfied dependency expressed through field ‘connection‘; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘getConnection‘ defined in class path resource [cn/chinaunicom/Config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.sql.Connection]: Factory method ‘getConnection‘ threw exception; nested exception is 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.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘getConnection‘ defined in class path resource [cn/chinaunicom/Config/BeanConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.sql.Connection]: Factory method ‘getConnection‘ threw exception; nested exception is 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.
这些报错都是来自同一个错误就是设置serverTimezone=UTC。把这个设置好了就好了。
但是我有一点不清楚:
我明明在.yml文件里写了这个配置,但是就读不出来,就必须在当时用的时候协商写好:
@Bean
public Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/text?serverTimezone=UTC", "root", "chen792979650");
return connection;
}
}
我配置文件里面的东西
server:
port: 9090
#配置内嵌服务器的端口
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
#数据库url
url: jdbc:mysql://localhost:3306/text?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
# 数据库访问账户
username: root
明明我配置了的,并且访问的时候也是用的9090端口,说明这个配置文件是生效的。但是为什么不行?
我猜测的原因:应该因为先后顺序的原因,在我debug的时候直接先对上面的连接数据库i的语句执行,再执行配置文件,但是再我先再连接数据库操作的时候已经出了问题,直接就报错了。
在与mysql的配置当中一定要注意的一点是设置serverTimezone=UTC
原文地址:https://www.cnblogs.com/chenjiaolong/p/13470895.html