最近在使用mysql的6.0.x以上的jar的時候,需要在代碼url的鏈接里面指定serverTimezone。就會出現異常:
1.未指定serverTimezone
xml里面配置url
1
|
< property name="url" value="jdbc:mysql://localhost:3306/mybatisstudy"/>
|
出現的異常
1
|
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配置屬性),如果您想要使用時區支持,則需要使用一個更詳細的時區值。
2.網上的解決方案
在url后面加上參數?serverTimezone=utc()
2.1.遇到的問題
雖然上面加上時區程序不出錯了,但是我們在用java代碼插入到數據庫時間的時候卻出現了問題。
比如在java代碼里面插入的時間為:2017-08-21 17:29:56
但是在數據庫里面顯示的時間卻為:2017-08-21 09:29:56
3.根本原因
因為時區設置的問題。
UTC代表的是全球標准時間 ,但是我們使用的時間是北京時區也就是東八區,領先UTC八個小時。
UTC + (+0800) = 本地(北京)時間
4.解決方案
url的時區使用中國標准時間。
第一種使用上海時間:serverTimezone=Asia/Shanghai
第二種使用北京時間:serverTimezone=GMT%2B8