数据库的连接池

 

 C3P0连接池

  C3P0的连接池使用

    

    @Test
    /**
     * 配置文件的使用,在src下 编写c3p0-config.xml文件就行了默认识别
     */
    public void c3p0() {
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet re = null;
        //创建连接池
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //建立连接
        try {
            conn = dataSource.getConnection();
            //编写sql
            String sql = "select * from emp";
            //预处理sql
            pstmt = conn.prepareStatement(sql);
            //获取查询结果集
            re = pstmt.executeQuery();
            while(re.next()) {
                System.out.print(re.getInt("id")+"-"+re.getString("username")+"-"+re.getString("sex")+"-"+re.getInt("age")+"-"+re.getString("addr"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn, pstmt, re);
        }
    }

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">root</property>
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">20</property>
  </default-config>
  
</c3p0-config>
原文地址:https://www.cnblogs.com/wuheng-123/p/13779647.html