1. 程式人生 > >java 通過eclipse編輯器用mysql嘗試 連接數據庫

java 通過eclipse編輯器用mysql嘗試 連接數據庫

odin repr cert 嘗試 行程 rust eat 技術分享 一次

註:本人學的是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,並再次運行程序,程序最終運行正常。結果如下:

技術分享

java 通過eclipse編輯器用mysql嘗試 連接數據庫