c3p0案例

1C3P0简介

  C3P0也是开源免费的连接池!C3P0被很多人看好!

2C3P0的使用

  C3P0中池类是:ComboPooledDataSource。

[java]  view plain  copy
  1. public void fun1() throws PropertyVetoException, SQLException {  
  2. ComboPooledDataSource ds = new ComboPooledDataSource();  
  3. ds.setJdbcUrl("jdbc:mysql://localhost:3306/mydb1");  
  4. ds.setUser("root");  
  5. ds.setPassword("123");  
  6. ds.setDriverClass("com.mysql.jdbc.Driver");  
  7. ds.setAcquireIncrement(5);  
  8. ds.setInitialPoolSize(20);  
  9. ds.setMinPoolSize(2);  
  10. ds.setMaxPoolSize(50);  
  11. Connection con = ds.getConnection();  
  12. System.out.println(con);  
  13. con.close();  
  14. }  


配置文件要求: 

l 文件名称:必须叫c3p0-config.xml

l 文件位置:必须在src


1.JdbcUtils.java

[java]  view plain  copy
  1. package cn.itcast.jdbc;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.SQLException;  
  5.   
  6. import javax.sql.DataSource;  
  7.   
  8. import com.mchange.v2.c3p0.ComboPooledDataSource;  
  9.   
  10. public class JdbcUtils {  
  11.     // 配置文件的默认配置!要求你必须给出c3p0-config.xml!!!  
  12.     private static ComboPooledDataSource dataSource = new ComboPooledDataSource();  
  13.       
  14.     /** 
  15.      * 使用连接池返回一个连接对象 
  16.      * @return 
  17.      * @throws SQLException 
  18.      */  
  19.     public static Connection getConnection() throws SQLException {  
  20.         return dataSource.getConnection();  
  21.     }  
  22.       
  23.     /** 
  24.      * 返回连接池对象! 
  25.      * @return 
  26.      */  
  27.     public static DataSource getDataSource() {  
  28.         return dataSource;  
  29.     }  
  30. }  

2.Demo3.java

[java]  view plain  copy
  1. package cn.itcast.dbutils;  
  2.   
  3. import java.sql.SQLException;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.apache.commons.dbutils.QueryRunner;  
  8. import org.apache.commons.dbutils.handlers.BeanHandler;  
  9. import org.apache.commons.dbutils.handlers.BeanListHandler;  
  10. import org.apache.commons.dbutils.handlers.MapHandler;  
  11. import org.apache.commons.dbutils.handlers.MapListHandler;  
  12. import org.apache.commons.dbutils.handlers.ScalarHandler;  
  13. import org.junit.Test;  
  14.   
  15. import cn.itcast.jdbc.JdbcUtils;  
  16.   
  17. public class Demo3 {  
  18.     @Test  
  19.     public void fun1() throws SQLException {  
  20.         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());  
  21.         String sql = "insert into t_stu values(?,?,?,?)";  
  22.         Object[] params = {1002"liSi"88"female"};  
  23.           
  24.         qr.update(sql, params);  
  25.     }  
  26.       
  27.     @Test  
  28.     public void fun2() throws SQLException {  
  29.         // 创建QueryRunner,需要提供数据库连接池对象  
  30.         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());  
  31.         // 给出sql模板  
  32.         String sql = "select * from t_stu where sid=?";  
  33.         // 给出参数  
  34.         Object[] params = {1001};  
  35.           
  36. //      ResultSetHandler<Stu> rsh = new ResultSetHandler<Stu>() {  
  37. //  
  38. //          @Override  
  39. //          public Stu handle(ResultSet rs) throws SQLException {  
  40. //              // TODO Auto-generated method stub  
  41. //              return null;  
  42. //          }  
  43. //      };  
  44.         // 执行query()方法,需要给出结果集处理器,即ResultSetHandler的实现类对象  
  45.         // 我们给的是BeanHandler,它实现了ResultSetHandler  
  46.         // 它需要一个类型,然后它会把rs中的数据封装到指定类型的javabean对象中,然后返回javabean  
  47.         Stu stu = qr.query(sql, new BeanHandler<Stu>(Stu.class), params);  
  48.         System.out.println(stu);  
  49.     }  
  50.       
  51.     /** 
  52.      * BeanListHandler的应用,它是多行处理器 
  53.      * 每行对象一个Stu对象! 
  54.      * @throws Exception 
  55.      */  
  56.     @Test  
  57.     public void fun3() throws Exception {  
  58.         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());  
  59.         String sql = "select * from t_stu";  
  60.         List<Stu> stuList = qr.query(sql, new BeanListHandler<Stu>(Stu.class));  
  61.           
  62.         System.out.println(stuList);  
  63.     }  
  64.       
  65.     /** 
  66.      * MapHandler的应用,它是单行处理器,把一行转换成一个Map对象 
  67.      * @throws SQLException  
  68.      */  
  69.     @Test  
  70.     public void fun4() throws SQLException  {  
  71.         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());  
  72.         String sql = "select * from t_stu where sid=?";  
  73.         Object[] params = {1001};  
  74.         Map map = qr.query(sql, new MapHandler(), params);  
  75.           
  76.         System.out.println(map);  
  77.     }  
  78.       
  79.     /** 
  80.      * MapListHandler,它是多行处理器,把每行都转换成一个Map,即List<Map> 
  81.      * @throws SQLException 
  82.      */  
  83.     @Test  
  84.     public void fun5() throws SQLException  {  
  85.         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());  
  86.         String sql = "select * from t_stu";  
  87.         List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler());  
  88.           
  89.         System.out.println(mapList);  
  90.     }  
  91.       
  92.     /** 
  93.      * ScalarHandler,它是单行单列时使用,最为合适! 
  94.      * @throws SQLException 
  95.      */  
  96.     @Test  
  97.     public void fun6() throws SQLException {  
  98.         QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());  
  99.         String sql = "select count(*) from t_stu";  
  100.         /* 
  101.          * Integer、Long、BigInteger 
  102.          */  
  103.         Number cnt = (Number)qr.query(sql, new ScalarHandler());  
  104.           
  105.         long c = cnt.longValue();  
  106.         System.out.println(c);  
  107.     }  
  108. }  

3.c3p0-config.xml(名字不能变)

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <c3p0-config>  
  3.     <!-- 这是默认配置信息 -->  
  4.     <default-config>   
  5.         <!-- 连接四大参数配置 -->  
  6.         <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb3</property>  
  7.         <property name="driverClass">com.mysql.jdbc.Driver</property>  
  8.         <property name="user">root</property>  
  9.         <property name="password">123</property>  
  10.         <!-- 池参数配置 -->  
  11.         <property name="acquireIncrement">3</property>  
  12.         <property name="initialPoolSize">10</property>  
  13.         <property name="minPoolSize">2</property>  
  14.         <property name="maxPoolSize">10</property>  
  15.     </default-config>  
  16.       
  17.     <!-- 专门为oracle提供的配置信息 -->  
  18.     <named-config name="oracle-config">   
  19.         <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property>  
  20.         <property name="driverClass">com.mysql.jdbc.Driver</property>  
  21.         <property name="user">root</property>  
  22.         <property name="password">123</property>  
  23.         <property name="acquireIncrement">3</property>  
  24.         <property name="initialPoolSize">10</property>  
  25.         <property name="minPoolSize">2</property>  
  26.         <property name="maxPoolSize">10</property>  
  27.     </named-config>  
  28.   
  29. </c3p0-config>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值