package tk.dong.connectionpool;import java.io.ioexception;import java.io.inputstream;import java.sql.connection;import java.sql.preparedstatement;import java.sql.resultset;import java.sql.sqlexception;import java.util.properties;import javax.sql.datasource;import org.apache.commons.dbcp.basicdatasourcefactory;public class pool_dbcp { // 首先要导入包commons-pool.jar和commons-dbcp-1.4.jar // 声明数据源 private static datasource datasource; private static preparedstatement pstmt; private static resultset rs; static { // 将配置文件以输入流的形式读入 inputstream inputstream = pool_dbcp.class.getclassloader() .getresourceasstream(dbcp.properties); // 创建属性操作的对象 properties properties = new properties(); try { // 将配置文件读入 properties.load(inputstream); // 创建基础的数据源处理工厂类的对象 basicdatasourcefactory basicdatasourcefactory = new basicdatasourcefactory(); // 通过基础的数据处理工厂创建出数据源 datasource = basicdatasourcefactory.createdatasource(properties); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } // 创建返回连接对象的方法 public static connection getconn() { try { return datasource.getconnection(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } public static void release(resultset rs, preparedstatement pstmt) { // 释放结果集 if (rs != null) { try { rs.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } // 释放准备语句 if (pstmt != null) { try { pstmt.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } } // 更新数据的操作增,删,改要用到的封装方法 public static boolean update(string sql, object[] obj) { boolean flag = false; try { // 准备语句的创建,带有sql命令的对象 pstmt = getconn().preparestatement(sql); for (int i = 1; i 0) { flag = true; } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } finally { release(rs, pstmt); } return flag; } // 进行批量删除处理 public static boolean updatebatchdel(string sql, object[] ids) { boolean flag = false; connection conn = getconn(); preparedstatement pstmt = null; resultset rs = null; try { conn.setautocommit(false); pstmt = conn.preparestatement(sql); for (int i = 0; i < ids.length; i++) { pstmt.setobject(1, ids[i]); system.out.println(sql + --------------- + ids[i]); pstmt.addbatch(); } int[] num = pstmt.executebatch(); // 批量执行 for (int i = 0; i < num.length; i++) { if (num[i] == 0) { try { conn.rollback(); // 进行事务回滚 return flag; } catch (sqlexception ex) { ex.printstacktrace(); } } } conn.commit();// 提交事务 flag = true; } catch (sqlexception e) { e.printstacktrace(); } finally { release(rs, pstmt); } return flag; } // 根据传入的表的名称,和每页数据得到传入表的所有的页数 // tablename:::::操作的数据表名称 // pagesize::::::每页显示的信息条数 public static integer getcountpage(string tablename, integer pagesize) { integer countpage = 0; string sql = select count(*) as c from + tablename; connection conn = pool_dbcp.getconn(); preparedstatement pstmt = null; resultset rs = null; conn = pool_dbcp.getconn(); try { pstmt = conn.preparestatement(sql); rs = pstmt.executequery(); if (rs.next()) { int countrecord = rs.getint(c); countpage = countrecord % pagesize == 0 ? countrecord / pagesize : countrecord / pagesize + 1; } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } finally { pool_dbcp.release(rs, pstmt); } return countpage; }}