c3p0配置Mysql数据源(加载配置文件方式)

jdbc.properties 至于resource
jdbc.properties 内容如下

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root


@Test
public void test1(){
/*
* c3p0配置Mysql数据源(加载配置文件方式)
*/
ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc.properties");
String driver = resourceBundle.getString("jdbc.driver");
String user = resourceBundle.getString("jdbc.username");
String password = resourceBundle.getString("jdbc.password");
String url = resourceBundle.getString("jdbc.url");
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setJdbcUrl(url);
dataSource.setDriverClass(driver);
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
} catch (PropertyVetoException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
posted @ 2021-02-24 13:30  year12  阅读(138)  评论(0编辑  收藏  举报