註:本人學的是Oracle,用mysql連接資料庫是一次嘗試。 一、下載JDBC mysql驅動,導入jar包 我自己下載的是connector-java-6.0.6.jar,如下圖所示,JDBC mysql驅動連接地址http://mvnrepository.com/artifact/mysql/ ...
註:本人學的是Oracle,用mysql連接資料庫是一次嘗試。
一、下載JDBC mysql驅動,導入jar包
我自己下載的是connector-java-6.0.6.jar,如下圖所示,JDBC mysql驅動連接地址http://mvnrepository.com/artifact/mysql/mysql-connector-java/6.0.6,
二、連接資料庫
代碼如下圖:
測試類:
然後我運行這個主方法,控制台出現如下異常:
之後我將異常:
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.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:545)
通過百度查找,知道我的Url地址不正確,並將其改為: private static String Url="jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
再次運行主方法,控制台結果如下:
接著我再按著控制台提示將驅動Driver="com.mysql.jdbc.Driver" 改為 Driver="com.mysql.cj.jdbc.Driver"
再次運行程式,控制台出現如下警告:
我再次將警告:Tue Nov 14 13:19:53 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
通過百度查找,最終將地址Url的值改為Url=jdbc:mysql://localhost:3306/crm?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false,並再次運行程式,程式最終運行正常。結果如下: