Java connect to the database using JDBC connector no longer need to load drivers

出错内容: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.

Download the mysql-connector-java-8.0.11.jar connect to the MySQL database and found a long list of red when loading drive, display the database connection is successful.

The reason lies in this statement:

Class.forName("com.mysql.jdbc.Driver");

Abnormality display, the drive is automatically registered by SPI, is not necessary to load the class driver.

Read this, I load the driver tried to remove the statement, it really solved the problem.

Test code:

/**
 * 
 */
package javatest;

import java.sql.*;

import sun.applet.Main;

/**
 * @author Administrator
 * @数据库封装
 */
public class JDBCConPackaging {
		public JDBCConPackaging(){
			Connection conn;
			Statement stmt;
			String user = "root";
			String pwd = "yuan";
			try{
//				Class.forName("com.mysql.jdbc.Driver");
				conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=FALSE",user,pwd);
				stmt = conn.createStatement();
				System.out.println("连接成功!!!");
			}catch(Exception e){
//				e.printStackTrace();
				System.out.println("连接出错");
			}
		}
		public static void main(String[] args) {
			new JDBCConPackaging();
		}
}

 

 

 

Published 35 original articles · won praise 30 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43792401/article/details/101638466