java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä'.. 問題解決方法
阿新 • • 發佈:2019-01-02
剛剛在java中連線Mysql資料庫的嘗試中,程式報出瞭如下的錯誤:
Exception in thread "main" 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.
仔細閱讀,發現是與時間設定相關,仔細研究後發現,是因為新版的Mysql中的時區預設設定與本地時區之間是不同的,因此會報錯。
由此,解決方法即為修改時區設定即可,可以在連線資料庫的url的最後新增這樣一段程式碼?serverTimezone=UTC
,就可以解決問題了。
如,我原先的程式碼為:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/javabook", "xxx", "xxxx");
System.out.println("Database connected!");
修改後為:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/javabook?serverTimezone=UTC", "lenfranky", "tiger");
System.out.println("Database connected!");
執行結果:
Database connected!