Failed to obtain JDBC Connection; nested exception is java.sql.SQLException

在spring连接数据库时,出现如下的错误:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
log4j:WARN No appenders could be found for logger (org.springframework.jdbc.datasource.DriverManagerDataSource).
log4j:WARN Please initialize the log4j system properly.
Mon Jan 18 16:28:35 CST 2021 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; 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.

经过上网查找的原因是:

使用mysql的jdbc驱动最新版(6.0+)时,会遇到数据库和系统时区差异引起的问题。

注:CST代表的是中国上海时间(与北京时间,东八区相同)。

解决方案是:

在 jdbc的url后加serverTimezone=CST

例如把原先的

data.setUrl("jdbc:mysql:///spring");

改成:

data.setUrl("jdbc:mysql:///spring?serverTimezone=CST");

就可以了!

猜你喜欢

转载自blog.csdn.net/qq_35207086/article/details/112787968
今日推荐