JDBC訪問MySql異常
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.
原因分析:
數據庫安裝時默認為英語,0:00時區
Windows系統中,XP的時區是GMT,而Win7的時區是UTC。
mysql返回的時間會比實際時間要早8小時。
解決方案,以下任選一種即可解決問題:
1、配置JDBC連接參數
在url連接字符串后面加上?serverTimezone=UTC
例如:
jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=UTC
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是一樣的,都與英國倫敦的本地時相同。
2、修改MySQL數據庫配置,需要DBA的root權限
使用root用戶登錄mysql
--查看時區值
show variables like '%time_zone%';
--設置為東八區(北京時間)
set global time_zone='+8:00';

3、有人說,使用低版本的MySQL jdbc驅動,5.1.47不會存在時區的問題,的確如此。
