mysql-connector-java 6版本的jdbc連接問題


使用新版本6的jdbc驅動,會出現下面的問題

Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 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.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_171]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_171]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_171]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_171]

就是未識別時區的問題,需要在jdbc連接的url上加上serverTimezone=UTC(世界標准時間)或者GMT(格林威治時間),寫成

   private String url = "jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC";

   private String username = "root";

   private String password = "root";

   private String driverClassName = "com.mysql.cj.jdbc.Driver";

關於SSL連接的Warning
SSL 是Secure Sockets Layer(安全套接層),可以在URL后面再添加一個屬性useSSL=false就行了
如果你不需要使用SSL連接,你需要通過設置useSSL=false來顯式禁用SSL連接。
如果你需要用SSL連接,就要為服務器證書驗證提供信任庫,並設置useSSL=true

 private String url = "jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC&useSSL=false";


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM