Eclipse中使用MySql遇到: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 o

时间:2023-03-09 17:59:25
Eclipse中使用MySql遇到: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 o

在Eclipse中使用MySQL遇到了点小问题

如果对Eclipse中配置MySql还有疑问的可以参考一下这篇博客:https://blog.****.net/qq_38247544/article/details/80419692

参考菜鸟上的例子的代码如下:

当然,这是修改后没问题后的代码

 package mysqltest;

 import java.sql.*;

 public class Mysql {
// jdbc驱动名以及数据库URL
// static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/javamysql" + "?serverTimezone=GMT%2B8"; // 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "root"; public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 注册JDBC驱动
Class.forName("com.mysql.jdbc.Driver"); // 打开链接
System.out.println("连接到数据库……");
conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询
System.out.println("实例化statement对象……");
stmt = conn.createStatement();
String sql;
sql = "SELECT id, name, url FROM websites";
ResultSet rs = stmt.executeQuery(sql); // 展开结果集数据库
while (rs.next()) {
// 通过字段检索
int id = rs.getInt("id");
String name = rs.getString("name");
String url = rs.getString("url"); // 输出数据
System.out.print("ID:" + id);
System.out.print(",站点名:" + name);
System.out.println(",站点URL:" + url);
} // 完成后关闭
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// 处理 JDBC错误
se.printStackTrace();
} catch (Exception e) {
// 处理Class.forname 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
} // 什么都不做
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}

遇到的问题如下:

Eclipse中使用MySql遇到: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 o

原来是因为使用最新的驱动包中`com.mysql.jdbc.Driver'类已经过时,新的`com.mysql.cj.jdbc.Driver'通过SPI自动注册,不再需要手动加载驱动类)

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.

但是后面还有一个如下的问题:

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.

找到了这篇博客

需要在数据库 URL中设置serverTimezone属性:(就是代码第八行)

static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB" + "?serverTimezone=GMT%2B8";  这里的 GMT%2B8 代表是东八区。(虽然不太明白为啥要加这个)

jdbc:mysql://localhost:3306/javamysql  端口号后面是你的数据库

最后问题解决了,如果有遇到相同问题的小伙伴可以参考一下!