@Test
public void testC3P0() throws IOException, PropertyVetoException, SQLException {
// 设置c3p0连接数据源
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
// 读取配置文件
Properties properties = new Properties();
properties.load(new FileInputStream("src\com\sky\chapter25\jdbc.properties"));
String user = (String) properties.get("user");
String password = (String) properties.get("password");
String url = (String) properties.get("url");
String driver = (String) properties.get("driver");
// 将连接的相关属性添加到DataSource
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(password);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setDriverClass(driver);
// 初始化连接数
comboPooledDataSource.setInitialPoolSize(10);
comboPooledDataSource.setMaxPoolSize(50);
Connection connection = comboPooledDataSource.getConnection();
System.out.println(connection);
connection.close();
}
// 将c3p0-config.xml放在src目录下
@Test
public void testC3P02() throws SQLException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("ywj");
Connection connection = comboPooledDataSource.getConnection();
System.out.println(connection);
}
com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/sky_db01?serverTimezone=UTC&useSSL=false root 1111 5 10 5 10 5 2
jdbc:mysql://localhost:3306/sky_db01?serverTimezone=UTC&useSSL=false 这个地方的url中‘;’在xml文件中会报错所以要是用'&'来代替分号
这里是c3p0的jar包和xml配置文件
链接:https://pan.baidu.com/s/1N1dHdb-LGzqqFqlO64V_gg
提取码:yjwt



