1. 程式人生 > >Mysql6.0連線中的幾個問題

Mysql6.0連線中的幾個問題

在最近做一些java web整合時,因為我在maven官網查詢的資源,使用的最新版,6.0.3,發現Mysql連線中的幾個問題,總結如下:


1、Loading class `com.mysql.jdbc.Driver'.This isdeprecated. The new driver class is `com.mysql.cj.jdbc.Driver。

按照書上的教程,寫了如下的資料庫連線配置:

user=root
password=
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/spring
以上配置中,密碼我是預設沒有設定的。在連線的時候出現以上提示,根據提示,很顯然,這種driver配置方式在此版本中已經被廢棄,因此需要將driverClass配置為:com.mysql.cj.jdbc.Driver。


2、如下提示:

警告: com[email protected]1a0e2e48 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
七月 05, 2016 8:58:29 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
警告: com
[email protected]
1a0e2e48 -- APPARENT DEADLOCK!!! Complete Status: 
...
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.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:695)

搜尋了一下 需要在Url後面新增一個引數:

?serverTimezone=UTC

即完整的配置應修改為:

user=root
password=
#old driver 
#driverClass=com.mysql.jdbc.Driver
#new driver is as follow:
driverClass=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/spring?serverTimezone=UTC

然後才能正常連線,加上後測試,連線成功。下圖是測試程式碼以及測試結果:



3、Url中localhost:3306 可以省略,看到有的地方直接寫的三個\\\,省略ip和埠,然後直接接某個資料庫。因此測試了一下,將配置改為:

user=root
password=
driverClass=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql:///spring?serverTimezone=UTC

發現也是可以連上的。


4、使用Hibernate整合連線mysql時,出現以下錯誤:

Disabling contextual LOB creation as createClob() method threw error : java. lang. reflect. InvocationTargetException。

原因: at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:244) ,就是在配置的時候沒取到值。那麼為什麼沒取到值呢,是因為Hibernate預設是使用jdbc方式來取的,如果使用連線池的方式的話,必須告訴Hibernate一聲,讓它不使用單純的JDBC連線。因此在Hibernate的hibernate.cfg.xml中加入一條屬性:

<property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
但是在加入此屬性時,有一點比較疑惑的是,在引號中輸入hibernate後使用自動補全,發現temp屬性沒有,不知道這是怎麼回事。截圖如下:


但直接複製以上程式碼進去,也不會出錯。。只能懷疑是eclipse補全功能的鍋了!