c3p0 JDBC连接池 xml配置文件的书写

需要创建  c3p0-config.xml 配置文件

  * c3p0配置文件
  * 1.配置文件名称:c3p0-config.xml
  * 2.配置文件的位置一定要在类路径下

复制

修改文件

首字母的大写改成小写

配置文件设置完成

记得导入数据库连接jar包

              c3p0  jar包

代码

 1 package test03;
 2 
 3 import java.beans.PropertyVetoException;
 4 import java.sql.Connection;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 import com.mchange.v2.c3p0.ComboPooledDataSource;
 9 
10 /**
11  * C3p0连接数据源
12  * @author star
13  *
14  */
15 public class C3p0Test {
16     public static void main(String[] args) throws Exception {
17         //test1();
18         test2();
19     }
20     
21     private static void test1() throws SQLException, PropertyVetoException {
22         // TODO Auto-generated method stub
23         //创建连接池
24         ComboPooledDataSource pool = new ComboPooledDataSource();
25         
26         //设置的连接的四大参数
27         //
28         pool.setDriverClass("com.mysql.jdbc.Driver");
29         pool.setJdbcUrl("jdbc:mysql:///day01");
30         pool.setUser("root");
31         pool.setPassword("root");
32         //
33         //获取连接
34         Connection conn = pool.getConnection();
35         String sql = "select * from stu";
36         ResultSet rs = conn.createStatement().executeQuery(sql);
37         while(rs.next()) {
38             System.out.println(rs.getInt(1)+"   "+rs.getString(2));
39         }
40         rs.close();
41         conn.close();
42     }
43     //使用配置文件
44     /*
45      * c3p0配置文件
46      * 1.配置文件名称:c3p0-config.xml 
47      * 2.配置文件的位置一定要在类路径下
48      */
49     private static void test2() throws Exception {
50         // TODO Auto-generated method stub
51         //c3p0 创建连接池对象
52         ComboPooledDataSource pool = new ComboPooledDataSource();
53         //获取连接
54         Connection conn = pool.getConnection();
55         String sql = "select * from stu";
56         ResultSet rs = conn.createStatement().executeQuery(sql);
57         while(rs.next()){
58             System.out.println(rs.getInt(1)+"    "+rs.getString(2));
59         }
60         rs.close();
61         conn.close();
62         
63     }
64     
65 }
c3p0连接池
 1 <c3p0-config>
 2   <default-config>
 3     <property name="driverClass">com.mysql.jdbc.Driver</property>
 4     <property name="jdbcUrl">jdbc:mysql:///day01</property>
 5     <property name="user">root</property>
 6     <property name="password">root</property>
 7     
 8 
 9   </default-config>
10 </c3p0-config>
c3p0-config.xml

//c3p0 创建连接池对象
  ComboPooledDataSource pool = new ComboPooledDataSource();

原文地址:https://www.cnblogs.com/star521/p/9026735.html