复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33@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); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28<c3p0-config> <named-config name="ywj"> <!-- 驱动类 --> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <!-- url--> <property name="jdbcUrl">jdbc:mysql://localhost:3306/sky_db01?serverTimezone=UTC&useSSL=false</property> <!-- 用户名 --> <property name="user">root</property> <!-- 密码 --> <property name="password">1111</property> <!-- 每次增长的连接数--> <property name="acquireIncrement">5</property> <!-- 初始的连接数 --> <property name="initialPoolSize">10</property> <!-- 最小连接数 --> <property name="minPoolSize">5</property> <!-- 最大连接数 --> <property name="maxPoolSize">10</property> <!-- 可连接的最多的命令对象数 --> <property name="maxStatements">5</property> <!-- 每个连接对象可连接的最多的命令对象数 --> <property name="maxStatementsPerConnection">2</property> </named-config> </c3p0-config>
复制代码
1
2
3jdbc:mysql://localhost:3306/sky_db01?serverTimezone=UTC&useSSL=false 这个地方的url中‘;’在xml文件中会报错所以要是用'&'来代替分号
这里是c3p0的jar包和xml配置文件
链接:https://pan.baidu.com/s/1N1dHdb-LGzqqFqlO64V_gg
提取码:yjwt
最后
以上就是标致钢笔最近收集整理的关于C3P0连接MySQL的全部内容,更多相关C3P0连接MySQL内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复