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

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

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

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

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

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

复制

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

修改文件

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

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

首字母的大写改成小写

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

配置文件设置完成

记得导入数据库连接jar包

              c3p0  jar包

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

 

 

代码

 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连接池

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2021-10-02
  • 2021-05-23
  • 2021-05-27
  • 2022-03-06
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2021-08-25
  • 2022-02-15
  • 2022-12-23
相关资源
相似解决方案