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.
时间: 2023-06-16 22:03:56 浏览: 178
这个警告信息是由于使用了过时的MySQL驱动程序而导致的。新的MySQL驱动程序是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'.
在提示信息中,显示了"Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'." 这句话表明,使用的数据库驱动`com.mysql.jdbc.Driver'已经被弃用,应该使用新的驱动`com.mysql.cj.jdbc.Driver'。根据提示信息,我们可以知道以前使用的驱动已经过时,建议更新为新的驱动。
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'
这个警告信息是因为使用了过时的 MySQL 驱动程序。新的 MySQL 驱动程序的类名为 `com.mysql.cj.jdbc.Driver`,建议使用新的驱动程序。
以下是使用新的 MySQL 驱动程序连接数据库的示例代码:
```java
import java.sql.*;
public class Main {
public static void main(String[] args) {
try {
// 注册 MySQL 驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取与 MySQL 数据库的连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai", "username", "password");
// 执行 SQL 查询
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
// 处理查询结果
while (rs.next()) {
// 读取查询结果中的数据
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
// 输出查询结果
System.out.println("id: " + id + ", name: " + name + ", age: " + age);
}
// 关闭连接
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
上述代码中,第 8 行调用了 `Class.forName("com.mysql.cj.jdbc.Driver")` 方法来注册 MySQL 驱动程序,第 11 行调用了 `DriverManager.getConnection` 方法来获取与 MySQL 数据库的连接。需要注意的是,在连接字符串中添加了 `useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai` 参数,以确保中文字符能够正确地显示。在获取到连接之后,就可以使用 `Statement` 对象执行 SQL 查询,并使用 `ResultSet` 对象来处理查询结果了。最后需要记得关闭连接。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![bat](https://img-home.csdnimg.cn/images/20210720083646.png)