前言
最近在使用mysql的6.0.x以上的jar的時候,需要在代碼url的鏈接里面指定serverTimezone。就會出現異常:
1.未指定serverTimezone
xml里面配置url
property name="url" value="jdbc:mysql://localhost:3306/mybatisstudy"/>
出現的異常
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: 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驅動程序(通過serverTimezone配置屬性),如果您想要使用時區(qū)支持,則需要使用一個更詳細的時區(qū)值。
2.網上的解決方案
在url后面加上參數?serverTimezone=utc
property name="url" value="jdbc:mysql://localhost:3306/springdatastudy?serverTimezone=UTC"/>
2.1.遇到的問題
雖然上面加上時區(qū)程序不出錯了,但是我們在用java代碼插入到數據庫時間的時候卻出現了問題。
比如在java代碼里面插入的時間為:2017-08-21 17:29:56
但是在數據庫里面顯示的時間卻為:2017-08-21 09:29:56
3.根本原因
因為時區(qū)設置的問題。
UTC代表的是全球標準時間 ,但是我們使用的時間是北京時區(qū)也就是東八區(qū),領先UTC八個小時。
UTC + (+0800) = 本地(北京)時間
4.解決方案
url的時區(qū)使用中國標準時間。也是就serverTimezone=Asia/Shanghai
4.1 使用java代碼獲取本地的時區(qū)id
Calendar cal = Calendar.getInstance(); TimeZone timeZone = cal.getTimeZone(); System.out.println(timeZone.getID()); System.out.println(timeZone.getDisplayName());
Asia/Shanghai 中國標準時間
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。