Using JDBC connection pool C3P0 demo

C3P0 connection pool

Demo- use manual configuration

public void demo(){
    // 获得连接:
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try{
        // 创建连接池:
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        // 设置连接池的参数:
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql:///test");
        dataSource.setUser("root");
        dataSource.setPassword("hello");
        dataSource.setMaxPoolSize(20);
        dataSource.setInitialPoolSize(3);
        
        // 获得连接:
        conn = dataSource.getConnection();
        // 编写Sql:
        String sql = "select * from user";
        // 预编译SQL:
        pstmt = conn.prepareStatement(sql);
        // 设置参数
        // 执行SQL:
        rs = pstmt.executeQuery();
        while(rs.next()){
            System.out.println(rs.getInt("uid")+"   "+rs.getString("username")+"   "+rs.getString("password")+"   "+rs.getString("name"));
        }
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        JDBCUtils.release(rs, pstmt, conn);
    }
}

Demo- profile configured to use

The main steps

  1. Create a c3p0-config.xmlprofile
  2. The definition of a class convenient tool to get connected and release resources
  3. Test connection pool class

Demo

C3P0 default XML file as a configuration file, the default search path the classpath c3p0-config.xmlfile, the actual configuration as follows

c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql:///test</property>
    <property name="user">root</property>
    <property name="password">hello</property>
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">20</property>
  </default-config>
  
</c3p0-config>

JDBCUnit.java

public class JDBCUtils {
    
    //创建连接池并设为静态常量
    private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
    
    // 用于获得连接
    public static Connection getConnection() throws Exception{
        Connection conn = dataSource.getConnection();
        return conn;
    }
    
    // 用于释放资源
    public static void release(Statement stmt,Connection conn){
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
    
    public static void release(ResultSet rs,Statement stmt,Connection conn){
        if(rs!= null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
}

This can be directly used in the test class

public void demo(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            /*// 获得连接:
            ComboPooledDataSource dataSource = new ComboPooledDataSource();*/
            // 获得连接:
            // conn = dataSource.getConnection();
            conn = JDBCUtils.getConnection();
            // 编写Sql:
            String sql = "select * from user";
            // 预编译SQL:
            pstmt = conn.prepareStatement(sql);
            // 设置参数
            // 执行SQL:
            rs = pstmt.executeQuery();
            while(rs.next()){
                System.out.println(rs.getInt("uid")+"   "+rs.getString("username")+"   "+rs.getString("password")+"   "+rs.getString("name"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils2.release(rs, pstmt, conn);
        }
    }

Guess you like

Origin www.cnblogs.com/esrevinud/p/11809520.html