数据库5.7-jdbc版本8.0.12驱动连接

现在版本的jdbc连接方式和原来不一样了,

假如你使用String driver = "com.mysql.jdbc.Driver";

会抛出错误:

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.

现在连接方式变了

修改方式两种

  1. String driver = "com.mysql.cj.jdbc.Driver";
  2. 去除Class.forName(driver); 字段
import java.sql.*;

import java.sql.Connection;

import org.junit.Test;
public class jdbc_1 {
    @Test
    public void Test() {
        //String driver = "com.mysql.cj.jdbc.Driver";
        String URL = "jdbc:mysql://localhost/exam?useSSL=false&serverTimezone = UTC&";
        try {
            
            //Class.forName(driver);                                                                        //这两行注释,要不都加上, 要不都不要加
            Connection con=DriverManager.getConnection(URL,"root","000000"); 
            System.out.println(con);
            
        }catch(Exception e){
            System.out.println("Connect fail:" + e.getMessage());
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/wuvkcyan/p/9428567.html