MySQL 基础19 C3P0开源连接池的使用

1.1 C3P0的连接池的概述

  C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。

1.2 C3P0的连接池的使用

1.2.1 手动设置参数的方式

配置文件说明

You can use the XML config file for all c3p0 configuration, including configuration of defaults. However, for users who don't want or need the extra complexity, the c3p0.properties file will continue to be supported.

By default, c3p0 will look for an XML configuration file in its classloader's resource path under the name "/c3p0-config.xml". That means the XML file should be placed in a directly or jar file directly named in your applications CLASSPATH, in WEB-INF/classes, or some similar location.

  • 名字为c3p0-config.xml
  • 直接放在src下即可
  • 当然了更多自定义查看doc的说明

@Test
    /**
     * 手动设置参数的方式:
     */
    public void demo1(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            // 获得连接:从连接池中获取:
            // 创建连接池:
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            // 设置连接参数:
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql:///web_test4");
            dataSource.setUser("root");
            dataSource.setPassword("abc");
            // 从连接池中获得连接:
            conn = dataSource.getConnection();
            // 编写SQL:
            String sql = "select * from account";
            // 预编译SQL:
            pstmt = conn.prepareStatement(sql);
            // 执行SQL:
            rs = pstmt.executeQuery();
            while(rs.next()){
                System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getDouble("money"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(rs, pstmt, conn);
        }
    }

1.2.2 采用配置文件的方式

配置文件



  
    
    com.mysql.jdbc.Driver
    jdbc:mysql:///my_test05
    root
    1234

    
      10
      1
      0
    

  

  
   
    50
    100
    50
    1000

    
    0 
    5

    
     
      1
      1
      1
      5
      50
    
  


使用连接池



@Test
    /**
     * 采用配置文件的方式:
     */
    public void demo2(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            // 获得连接:从连接池中获取:
            // 创建连接池://创建连接池默认去类路径下查找c3p0-config.xml
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            // 从连接池中获得连接:
            conn = dataSource.getConnection();
            // 编写SQL:
            String sql = "select * from account";
            // 预编译SQL:
            pstmt = conn.prepareStatement(sql);
            // 执行SQL:
            rs = pstmt.executeQuery();
            while(rs.next()){
                System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getDouble("money"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(rs, pstmt, conn);
        }
    }

你可能感兴趣的:(MySQL 基础19 C3P0开源连接池的使用)