1. 程式人生 > >Mysql 5.5和JDBC連線時出現的時區問題

Mysql 5.5和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.
問題顯示:伺服器時區的值無法識別或者代表一個以上的時區,如果想要使用時區支援,必須配置伺服器或JDBC驅動程式(通過serverTimezone配置屬性)去使用一個更詳細的時間。

既然問題已經明白,那麼修改程式碼即可

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?serverTimezone=UTC","root","");

然後就能成功連線Mysql了

問題二:那就是現在Mysql5.5已經不支援用下面這種的方式獲取驅動了

Class.forName("com.mysql.jdbc.Driver");

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

上面提示說,已經不贊成載入“com.mysql.jdbc.Driver”這個類,新的驅動類為“com.mysql.cj.jdbc.Driver”。
Class.forName("com.mysql.cj.jdbc.Driver");

問題三:java程式碼將資料寫入到資料庫中亂碼的問題

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?serverTimezone=UTC
                              &useUnicode=true&characterEncoding=utf-8","root","");
好啦,問題解決了,目前還在學習中