我在做spring项目时出现了一些jdbc连接连接问题
问题一:
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:122)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1714)
... 13 more
原因:这是因为没有导入mysql-connector-java.jar这个文件
解决:然后我在mysql官网上下载了mysql-connector-java-8.0.11.zip,将其解压后,把mysql-connector-java-8.0.11.jar导入到我的spring项目中。
这是下载地址:https://dev.mysql.com/downloads/connector/j/
修改后又出现问题二:
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.
原因:这个错误是因为spring jdbc项目中使用的是mysql-connector-java-8.0.11.jar,这个版本的xml配置文件的驱动类属性值应该是这样<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
解决:只要把<property name="driverClassName" value="com.mysql.jdbc.Driver"/>改成<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>就好。
改完运行后出现问题三:
Exception in thread "main" 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版本是5.5版本,和这个mysql-connector-java-8.0.11.jar版本不匹配
解决:在MySQL官网下载对应版本,我的就是mysql-connector-java-5.1.47.jar,用这个jar包就得配置文件就应该改回来<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
下载地址:https://dev.mysql.com/downloads/file/?id=480091