添加jdbc.properties文件 mysql8.0版本
url=jdbc:mysql://localhost:3306/DATAName?serverTimezone=GMT%2B8&characterEncoding=utf-8
&useSSL=false&allowPublicKeyRetrieval=true
driverClassName=com.mysql.cj.jdbc.Driver //驱动
username=root //数据库用户名
password=1234 //密码
mysql5版本
url=jdbc:mysql://localhost:3306/DATAName
driverClassName=com.mysql.jdbc.Driver //驱动
username=root //数据库用户名
password=1234 //密码
测试
@Test
public void testConnection5() throws Exception {
//1.加载配置文件 InputStream is =
ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties pros = new Properties();
pros.load(is);
//2.读取配置信息
String username = pros.getProperty("username");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClassName = pros.getProperty("driverClassName");
//3.加载驱动 Class.forName(driverClassName);
//4.获取连接
Connection conn = DriverManager.getConnection(url,username,password);
System.out.println(conn);
}