--------------java代码
public Connection getConnection2() throws Exception{
//1,准备数据库连接的4个条件
String driverClass =null;
String url =null;
String user=null;
String password=null;
//读取这个四个数据
InputStream in =
this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
driverClass=properties.getProperty("driverClass");
url=properties.getProperty("url");
user=properties.getProperty("user");
password=properties.getProperty("password");
//2,加载数据库连接的驱动程序
Class.forName(driverClass);
//3,获取数据的连接
Connection connection =DriverManager.getConnection(url,user,password);
return connection;
}
------------------properties
#driver=oracle.jdbc.driver.OracleDriver
#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
#user=scott
#password=java
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/atguigu
user=root
password=1230
---------------------补充内容
从配置文件加载数据库连接
本文详细介绍了如何从配置文件中加载数据库连接,包括读取配置文件、加载数据库驱动、建立连接等步骤。
5111

被折叠的 条评论
为什么被折叠?



