JDBC连接数据库时错误提示的解决方案汇总

今天在连接JDBC时,出现了错误

最开始的URL是这样写的

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/alibaba

报错为:

1.Establishing SSL connection without server's identity verification is not recommended

原因:mysql版本过高创建连接 

解决办法:在mysql连接上加上&useSSL=true

2.Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.

解决方法:由于mysql 的版本过高 需要将原来的加载驱动改为:class.forName("com.mysql.cj.jdbc.Driver")

3.报错为:The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone

出现这个的原因是因为 mysql返回的时间总是有问题,比实际时间要早8小时。

 在jdbc连接的url后面加上serverTimezone=GMT即可解决问题,如果需要使用gmt+8时区,需要写成GMT%2B8

最后URL变为:jdbc:mysql://localhost:3306/alibaba?serverTimezone=GMT&useSSL=false

 

当然网上也有更完善的版本:String url = "jdbc:mysql://localhost:3306/test_10?useUnicode=true&characterEncoding=utf-8&useSSL=false&&serverTimezone=GMT";

 

目前发现了一篇博客详尽解释了上述问题:https://www.cnblogs.com/Mimick/p/9011003.html

4.今天连接JDBC时新发现了一个错误:Public Key Retrieval is not allowed

最简单的解决方法:在URL后面加上&&allowPublicKeyRetrieval=true 完美解决

//-------------------------------------------------------------------------------------------------------------//

1.今天用JUnit测试时屡屡报错 总结一下:@Test一定要加,并且一定要加在测试方法的上面那一行

加错了位置会报错

2.今天使用DBCP数据库连接池爆出了
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFact的错误

原因:没有拷贝common-logging.jar

至此:连接DBCP数据库时需要拷贝三个jar包(缺一不可):common-dbcp.jar、common-pool2.jar、common-logging.jar

3.只有在测试类中才能用JUnit进行测试 其他类是不可以的

并且可以仅对选中的测试方法进行测试

posted @ 2018-07-29 22:33  AmosWong  阅读(1476)  评论(0编辑  收藏  举报