关于java连接mysql8.0因为版本不匹配报错的那些事

一开始驱动版本是:5.1

mysql-connector-java-5.1.28-bin.jar

此时连接mysql8.0,会报错:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.


排除数据库问题和语句问题后百度,发现可能是版本问题,于是下载了新版本 mysql-connector-java-8.0.16.jar

依然报错,不过比之前少了7个(滑稽):Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.

此时是语句问题:要在端口号3306/数据库名字后面添加?serverTimezone=GMT%2B8,

例如下面:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db?serverTimezone=GMT%2B8","root","password");

这次终于没有报错orz

原文地址:https://www.cnblogs.com/dabuliu/p/13674255.html