第一步自定义 properties 文件
user=root
password=12345
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
driverClass=com.mysql.jdbc.Driver
第二部 创建 java 文件运行
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;import org.junit.Test;public class ConnectionTest {@Testpublic void getConnection5() throws Exception{//1.读取配置文件中的4个基本信息InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
// System.out.println(is);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");
//
// //2.加载驱动Class.forName(driverClass);
//
// //3.获取连接Connection conn = DriverManager.getConnection(url, user, password);System.out.println(conn);}
}
效果展示: