问题遇到的现象和发生背景
Java编程软件:eclipse
数据库环境:MySQL社区版8.0.29
配套驱动jar版本:mysql-connecter-java-8.0.29.jar
问题相关源代码
package day01.connection;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
public class Connection {
@Test
public void testConnection1() throws ClassNotFoundException, SQLException, IOException{
//1.加载配置文件
InputStream is = Connection.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
//2.读取配置信息
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
//3.加载驱动
Class.forName(driverClass);
//4.获取连接
Connection conn = (Connection) DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
}
配置的jdbc.properties内容如下:
user=root
password=123456789 //不便展示
url=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.cj.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.
com.mysql.cj.jdbc.ConnectionImpl@57576994
我的解答思路和尝试过的方法
我也不清楚这是成功了没,因为是第一天学。
虽然JUnit没报错,但是Console里面老是标红,挺没底的,想问问大家这算是连接成功了吗?
我想要达到的结果
如果没连接成功,需要怎么改啊?