1. 程式人生 > >JDBC學習筆記

JDBC學習筆記

unicode oca nco serve utf-8 enc mys val class

今天在連接JDBC時,出現了錯誤

最開始的URL是這樣寫的

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/alibaba

報錯為:

1.Establishing SSL connection without server‘s identity verification is not recommended

原因:mysql版本過高創建連接

解決辦法:在mysql連接上加上&useSSL=true

2.Loading class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver‘.

解決方法:由於mysql 的版本過高 需要將原來的加載驅動改為:class.forName("com.mysql.cj.jdbc.Driver")

3.報錯為:The server time zone value ‘???ú±ê×??±??‘ is unrecognized or represents more than one time zone

出現這個的原因是因為 mysql返回的時間總是有問題,比實際時間要早8小時。

在jdbc連接的url後面加上serverTimezone=GMT即可解決問題,如果需要使用gmt+8時區,需要寫成GMT%2B8

最後URL變為:jdbc:mysql://localhost:3306/alibaba?serverTimezone=GMT&useSSL=false

當然網上也有更完善的版本:String url = "jdbc:mysql://localhost:3306/test_10?useUnicode=true&characterEncoding=utf-8&useSSL=false&&serverTimezone=GMT";

JDBC學習筆記