1.使用ResourceBundle.getBundle()获取properties文件 注意参数是基本名称,不需要写后缀
@Test
public void aa(){
ResourceBundle rs = ResourceBundle.getBundle("aa");
String username = rs.getString("username");
String password = rs.getString("password");
System.out.println(username+"========="+password);
}
2.链接c3p0连接池
@Test
public void getDruidConnection() throws PropertyVetoException, SQLException {
ResourceBundle rs = ResourceBundle.getBundle("druid");
String driverClassName = rs.getString("driverClassName");
String url = rs.getString("url");
String username = rs.getString("username");
String password = rs.getString("password");
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass(driverClassName);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(username);
comboPooledDataSource.setPassword(password);
Connection connection = comboPooledDataSource.getConnection();
System.out.println(connection);
}
3. 链接druid 连接池
@Test
public void getC3p0Connection() throws SQLException {
ResourceBundle rs = ResourceBundle.getBundle("druid");
String driverClassName = rs.getString("driverClassName");
String url = rs.getString("url");
String username = rs.getString("username");
String password = rs.getString("password");
// 创建数据库源对象 设置链接参数
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
DruidPooledConnection connection = druidDataSource.getConnection();
System.out.println(connection);
}