我在做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