java在連接mysql數據庫時,會由於時區設置不正確導致報以下的錯誤:
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.
這種情況下可以修改數據庫的時區。
解決方法有二:
一、命令行方式(臨時性的解決)
使用管理員啟動CMD,進入mysql數據庫,輸入命令:
show variables like "%time_zone%";
一般會得到以下結果:
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set, 1 warning (0.00 sec)
這時候輸入命令
set global time_zone='+8:00';
設置完成后,重啟mysql服務。
再次驗證
show variables like "%time_zone%";
結果
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | |
| time_zone | +08:00 |
+------------------+--------+
2 rows in set, 1 warning (0.00 sec)
重新運行java程序即可正確連接數據庫。
二、修改my.ini下的【mysqld】(永久解決)
打開my.ini在【mysqld】下增加一句
default-time_zone = ‘+8:00’
保存退出后。重啟MySQL服務。
然后使用java程序即可連接mysql數據庫。