JDBC连接MySQL8.0+遇到的问题

JDBC连接MySQL8.0+遇到的问题

为了尝个鲜,安了MySQL8.0.13,在JDBC连接的时候遇到各种问题

首先是需要使用对应版本的JDBC驱动,推荐到阿里云maven仓库下载。

http://maven.aliyun.com/mvn/search
(点文件名,可以查看XML坐标)


新建一个Maven工程
XML坐标复制到工程的pom.xml文件中
在文件源码里面 project标签内部 添加<dependecies>标签,把复制的内容粘贴到里面 ,保存 在工程中出现"奶瓶"即可

pom.xml代码示例:


  4.0.0
  你的groupId
  你的artifactId
  0.0.1-SNAPSHOT
  
  
    mysql
    mysql-connector-java
    8.0.13

	
    commons-dbcp
    commons-dbcp
    1.4

  

创建个Demo类测试

mysql8.0+ JDBC语句和旧版本的不一样,写旧语法会报错

	//1.注册驱动
	Class.forName("com.mysql.cj.jdbc.Driver");//mysql8.0+写法
	
	//2. 获取连接对象
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/你要加载的库名?serverTimezone=UTC","root","你的mysql密码");//mysql8.0+写法
	//测试是否连上
	System.out.println(conn);

ps:在url中添加多字段时要用&连接


## 报错: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);”


祝你编程愉快!

你可能感兴趣的:(Java,JDBC,MySQL)