0
votes

My Code:

file ConnectionTest.java
package com.test.connection;

import java.io.InputStream;

import java.sql.*;
import java.util.Properties;

import org.junit.Test;



public class ConnectionTest {
@Test
public void getConnectTest5() throws Exception {
    //1、读取配置文件中的基本信息
    InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
    
    Properties pros = new Properties();
    pros.load(is);
    
    String user = pros.getProperty("user");
    String password = pros.getProperty("password");
    String url = pros.getProperty("url");
    String driverClass = pros.getProperty("driverClass");
    
    Class.forName("com.mysql.cj.jdbc.Driver");
    
    Connection conn = DriverManager.getConnection(url, user, password);
    System.out.println(conn);
} 

}

file jdbc.properties:

user=root password=Aa123456 url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false driverClass=com.mysql.cj.jdbc.Driver

1
I think you forgot to ask a question.Scratte
Just remove the Class.forName() call. It is not needed anymore, and hasn't been needed by compliant drivers since Java 6, so hasn't been needed since 2007.Andreas
When I remove Class.forName(),I still get the same errorStudent

1 Answers

-1
votes

My Code Error: 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.