1. 程式人生 > >mysql新版的jdbc驅動配置

mysql新版的jdbc驅動配置

新版的mysqljdbc驅動jar包的配置與之前發生了改變,今天在配置mybatis中mysql驅動的時候一直報錯:

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: 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.
### The error may exist in userMapper.xml
### The error may involve com.ycc.mybatisStudy.userMapper.selectUser
### The error occurred while executing a query
### Cause: 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.
 .....

自己也是檢查了半天的jdbc的配置也沒有發現什麼問題,後來在網上百度了一下發現是jdbc的配置發生了改變

老版配置mysqljdbc驅動:

<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisdb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>

新版配置:

<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?

useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>

關於mysqljdbc驅動的URL引數,常用的有一些引數如下表所示:

引數名稱 引數說明 預設值 最低版本要求
user 資料庫使用者名稱(用於連線資料庫) 所有版本
password 使用者密碼(用於連線資料庫) 所有版本
useUnicode 是否使用Unicode字符集,如果引數characterEncoding設定為gb2312或gbk,本引數值必須設定為true false 1.1g
characterEncoding 當useUnicode設定為true時,指定字元編碼。比如可設定為gb2312或gbk false 1.1g
autoReconnect 當資料庫連線異常中斷時,是否自動重新連線? false 1.1
autoReconnectForPools 是否使用針對資料庫連線池的重連策略 false 3.1.3
failOverReadOnly 自動重連成功後,連線是否設定為只讀? true 3.0.12
maxReconnects autoReconnect設定為true時,重試連線的次數 3 1.1
initialTimeout autoReconnect設定為true時,兩次重連之間的時間間隔,單位:秒 2 1.1
connectTimeout 和資料庫伺服器建立socket連線時的超時,單位:毫秒。 0表示永不超時,適用於JDK 1.4及更高版本 0 3.0.1
socketTimeout socket操作(讀寫)超時,單位:毫秒。 0表示永不超時 0 3.0.1

需要注意的是,在xml配置檔案中,url中的&符號需要轉義成&。需要在&後新增amp;