JDBC建立连接常见错误------url路径错误
第一个地方:Class.forName(“XXX”)中的错误
比如高版本的jar包的填写内容是:Class.forName(“com.mysql.cj.jdbc.Driver”);
如果出现下表示的错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client
该 问题可能是因为自己使用的mysql版本过高,需要跟换更高版本的jar包,我这里是去官网下载的8.0版本的jar包:
https://dev.mysql.com/downloads/file/?id=477058
点击下图的地方:
下载的文件需要解压,jar包的位置如图:
SSL设置问题
运行连接,如果还是爆红出现如下的问题:
Tue Feb 21 10:47:32 CST 2023 WARN: Establishing SSL connection without serve
出现该问题的原因很可能是因为:没有禁用SSL,设置useSSL=false
如:useSSL=false
String url="jdbc:mysql://localhost:3306/mypri?useUnicode=true&characterEncoding=utf-8&useSSL=false";
serverTimezone=UTC设置时区问题
如果出现报错:java.sql.SQLException: The server time zone value 'Öйú±ê×¼
那应该是时区设置的问题,现需要设置时区;
如我的写法:
String url="jdbc:mysql://localhost:3306/mypri?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
=utf-8&useSSL=false&serverTimezone=UTC";
这样运行就基本没有什么问题了