DBCP数据库连接池的使用

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 <= obj.length; i++) {
				pstmt.setObject(i, obj[i - 1]);
			}
			int i = pstmt.executeUpdate();
			if (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;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值