使用高版本mysql-connector-java的時區問題


錯誤信息

今天在復習 JavaEE 相關內容的時候,遇到報錯

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.

然后發現他的driver也改成com.mysql.cj.jdbc.Driver

原因

​ 驅動包版本:mysql-connector-java-8.0.18.jar

​ 后來在查資料后發現數據庫安裝時默認為英語,是0:00時區,但是用以前舊版本的MySql jdbc 驅動都不會讓你設置時區,新版就需要了。。。。

解決方法

將連接數據庫的url加上,UTC(世界均衡時間)參數,或者GMT(格林尼治時間),如下:

jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=UTC
jdbc:mysql://localhost:3306/demo?serverTimezone=GMT
jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=GMT

不過后來測試發現,這個時間比北京時間早8個小時,所以使用北京時間如下:

jdbc:mysql://localhost:3306/demo?serverTimezone=GMT%2B8
jdbc:mysql://127.0.0.1:3306/demo?serverTimezone=GMT%2B8

更多內容大家可以訪問我的個人博客:一只大大怪


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM