JDBC連線MySQL8.0+遇到的問題
JDBC連線MySQL8.0+遇到的問題
為了嚐個鮮,安了MySQL最新版,在JDBC連線的時候遇到問題了,
首先是需要使用對應版本的JDBC驅動,推薦到阿里雲maven倉庫下載。
http://maven.aliyun.com/mvn/search
(點檔名,可以檢視XML座標)
新建一個Maven工程,
把XML座標複製到工程的pom.xml檔案中 ,在 檔案原始碼裡面 project標籤內部 新增<dependecies>標籤,把複製的內容貼上到裡面 ,儲存
在工程中出現"奶瓶"即可
pom.xml程式碼示例:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>你的groupId</groupId> <artifactId>你的artifactId</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> </dependencies> </project>
建立個Demo類測試(注意看註釋)
//1.註冊驅動 //Class.forName("com.mysql.jdbc.Driver");//mysql5.0+寫法 Class.forName("com.mysql.cj.jdbc.Driver");//mysql8.0+寫法 //2. 獲取連線物件 //Connection conn = DriverManager.getConnection //("jdbc:mysql://localhost/你要載入庫名","root", "你的密碼");//mysql5.0+寫法 Connection conn = DriverManager.getConnection ("jdbc:mysql://localhost/你要載入的庫名?serverTimezone=UTC","root","你的密碼");//mysql8.0+寫法 //測試是否連上 System.out.println(conn);
ps:在url中新增多欄位時要用&連線 例如:
Connection conn =
DriverManager.getConnection
("jdbc:mysql://localhost/你要載入的庫名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC","root","你的密碼");
報錯:
“Establishing SSL connection withoutserver'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 defaultif explicit option isn't set. For compliance with existing applications notusing SSL the verifyServerCertificate property is set to 'false'. You needeither to explicitly disable SSL by setting useSSL=false, or set useSSL=trueand provide truststore for server certificate verification.”
解決方案:在url中加上
“useUnicode=true&characterEncoding=utf-8&useSSL=false”
Connection conn =
DriverManager.getConnection
("jdbc:mysql://localhost/你要載入的庫名?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC","root","你的密碼");
(mysql-connector-java-8.0.13 版本不會出現以上錯誤) 。
報錯:
“java.sql.SQLException: The server time zonevalue '???ú±ê×??±??' is unrecognized or represents more than one time zone. Youmust configure either the server or JDBC driver (via the serverTimezoneconfiguration property) to use a more specifc time zone value if you want toutilize time zone support.”
解決方案:確保url中加上“serverTimezone=UTC”
。
如果出現這個錯誤:
Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password:Yes or No);
解決方案:確保密碼輸入正確。
報錯:
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.StatementImpl.checkForDml(StatementImpl.java:385)
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1153)
at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)
at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)
at cn.tedu.Demo01.insert(Demo01.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
解決方案:將“stat.executeQuery(sql);”換成“stat.execute(sql);”
…