JDBC连MySQL出错
- 问题描述
-
- 查看MySQL版本
- 引入mysql-connector-java.jar
- 写Java代码
- 报错
- 修改代码
- 运行成功
问题描述
以下所有内容均是windows下操作
今天本着复习JDBC这块知识,想着对面试也有帮助,创建了maven项目,引入了mysql-connector-java.jar,结果创建完成后报错。
The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone.
You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
查看MySQL版本
(前提是设置了mysql到环境变量path)
mysql -V
其它查看mysql版本的方式
引入mysql-connector-java.jar
pom.xml:
<!-- 跟自己mysql版本选着对应的驱动版本 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
写Java代码
package ssm.jdbc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
public class JDBCTest {
private static final Logger log = LoggerFactory.getLogger(JDBCTest.class);
public static void main(String[] args) {
try {
String url = "jdbc:mysql://localhost:3306/spider";
String username = "root";
String passwd = "123456";
// 注册驱动
Class.forName("com.mysql.jdbc.Driver");
Connection connection = null;
Statement statement = null;
// 获取连接对象
try {
connection = DriverManager.getConnection(url,username,passwd);
statement = connection.createStatement();
// 创建sql执行对象
String sql = "select id,name from user";
//执行sql语句
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
User u = new User(id,name);
log.info(u.toString());
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
// 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
报错
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
...
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
...
Exception in thread "main" java.lang.NullPointerException
at ssm.jdbc.JDBCTest.main(JDBCTest.java:39)
Process finished with exit code 1
修改代码
将JDBC的url属性修改:
运行成功
运行是成功了,但是有警告:将old driver
com.mysql.jdbc.Driver
替换成new driver
com.mysql.cj.jdbc.Driver
作为一个强迫症,果断根据提示替换驱动名,然后再运行,发现提示消失。
// Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");