JDBC報服務器時間錯誤

在進行數據庫連接的過程中,出現瞭如下報錯:

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.

報錯代碼如下:

String url = "jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=UTF8";
String user = "root";
String password = "root";
Connection con = DriverManager.getConnection(url, user, password);   //此行報錯

使用的mysql版本爲8.0.15.0,jdbc驅動版本爲8.0;

在網上查找解決辦法,得知這是由於數據庫和系統時區差異所造成的,在JDBC連接的URL後面加上serverTimezone = GMT即可解決問題,如果需要使用GMT + 8時區,需要寫成GMT%2B8(2B即"+“的十六進制ASCII碼,”%2B"即“+”),否則會被解析爲空。

修改後url如下:

String url = "jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT";
或
String url = "jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8";

GMT(Greenwich Mean Time):格林尼治時間爲世界時,北京時間=GMT+8小時。
2B即"+“的十六進制ASCII碼,”%2B"即“+”。

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