JDBC訪問MySql異常java
Exception in thread "main" 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.mysql
緣由分析:sql
數據庫安裝時默認爲英語,0:00時區數據庫
Windows系統中,XP的時區是GMT,而Win7的時區是UTC。apache
mysql返回的時間會比實際時間要早8小時。
url
解決方案,如下任選一種便可解決問題:code
一、配置JDBC鏈接參數server
在url鏈接字符串後面加上?serverTimezone=UTCblog
例如:
jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=UTCci
jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC
學點新知識:
UTC,世界均衡時間
GMT,格林尼治時間
北京時間(東八區),GMT+8,url中表示爲:&serverTimezone=GMT%2B8
jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=GMT%2B8
咱們通常認爲GMT和UTC是同樣的,都與英國倫敦的本地時相同。
二、修改MySQL數據庫配置,須要DBA的root權限
使用root用戶登陸mysql
--查看時區值
show variables like '%time_zone%';
--設置爲東八區(北京時間)
set global time_zone='+8:00';
三、有人說,使用低版本的MySQL jdbc驅動,5.1.47不會存在時區的問題,的確如此。