使用高版本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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章