JDBC

package nc.jdbc.framework;

import java.util.ArrayList;
import nc.jdbc.framework.JdbcSession;
import nc.jdbc.framework.JdbcTransaction;
import nc.jdbc.framework.PersistenceManager;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.exception.DbException;
import nc.jdbc.framework.processor.ResultSetProcessor;

/**
 * JDBC 封装
 * @author wuhaidong
 * @version 1.0
 */
public class DBUtils {
	
	private PersistenceManager sessionManager = null;
	private JdbcSession session = null;
	private JdbcTransaction tran = null;
	private Object obj = null;
	/**
	 * 构造函数
	 * 描述: 管理连接会话的生命周期,并提供了对单表VO操作的常用实现
	 */
	public DBUtils(){
		try {
			sessionManager = PersistenceManager.getInstance();
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 构造函数
	 * 描述: 根据传递构造参选择不同的数据源
	 * @param dataSource
	 */
	public DBUtils(String dataSource){
		try {
			if(null != dataSource && !dataSource.equals("")){
				sessionManager = PersistenceManager.getInstance(dataSource);
			}
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 构造函数
	 * 描述: 根据传递构造参选择不同的连接(JdbcSession)
	 * @param session
	 */
	public DBUtils(JdbcSession session){
		if(null != session){
			sessionManager = PersistenceManager.getInstance(session);
		}
	}
	/**
	 * 提供了对单表VO操作的常用实现
	 * @return
	 */
	public PersistenceManager getPersistenceManager(){
		return sessionManager;
	}
	/**
	 * 连接数据库
	 * @throws DbException 
	 *
	 */
	public JdbcSession getConnection() throws DbException{
		if(null == session){
			session = sessionManager.getJdbcSession();
		}
		return session;
	}
	/**
	 * 执行数据库操作 -- 增,删,改
	 * @param sqlStr
	 * @param param
	 * @return
	 */
	public boolean executeUpdate(String sqlStr,SQLParameter param){
		if(null == sqlStr || sqlStr.length() == 0){
			return false;
		}
		if(null == param){
			return false;
		}
		try {
			this.getConnection();
			session.executeUpdate(sqlStr,param);
			return true;
		} catch (DbException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}finally{
			sessionManager.release();//需要关闭会话
		}
	}
	/**
	 * 执行数据库操作 -- 增,删,改
	 * @param sqlStr
	 * @return
	 */
	public boolean executeUpdate(String sqlStr){
		if(null == sqlStr || sqlStr.length() == 0){
			return false;
		}
		try {
			this.getConnection();
			session.executeUpdate(sqlStr);
			return true;
		} catch (DbException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}finally{
			sessionManager.release();//需要关闭会话
		}
	}
	/**
	 * 执行数据库操作 -- 查
	 * @param sqlStr
	 * @param rsProcessor
	 * @return
	 */
	public Object executeQuery(String sqlStr, ResultSetProcessor rsProcessor){
		if(null == sqlStr || sqlStr.length() == 0){
			return null;
		}
		try {
			this.getConnection();
			session.setReadOnly(true);//设置对数据库只读模式
			obj = session.executeQuery(sqlStr, rsProcessor);
			session.setReadOnly(false);//设回对数据库的模式
			return obj;
		} catch (DbException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}finally{
			sessionManager.release();//需要关闭会话
		}
	}
	/**
	 * 执行数据库操作 -- 查
	 * @param sqlStr
	 * @param param
	 * @param rsProcessor
	 * @return
	 */
	public Object executeQuery(String sqlStr, SQLParameter param, ResultSetProcessor rsProcessor){
		if(null == sqlStr || sqlStr.length() == 0){
			return null;
		}
		if(null == param){
			return null;
		}
		try {
			this.getConnection();
			session.setReadOnly(true);//设置对数据库只读模式
			obj = session.executeQuery(sqlStr, param, rsProcessor);
			session.setReadOnly(false);//设回对数据库的模式
			return obj;
		} catch (DbException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}finally{
			sessionManager.release();//需要关闭会话
		}
	}
	/**
	 * 多条数据库更新 -- 事务
	 * @param al
	 * @return
	 */
	public boolean executeUpdates(ArrayList<String> al){
		if(null == al && al.size() == 0){
			return false;
		}
		try {
			this.getConnection();
			session.setAutoCommit(false);//不自动提交模式
			tran = session.createTransaction();//开始事务
			for(int i = 0; i < al.size(); i++){
				session.addBatch(al.get(i));
			}
			session.executeBatch();
			tran.commitTransaction();//提交事务
			session.setAutoCommit(true);//设回自动提交模式
			return true;
		} catch (DbException e) {
			e.printStackTrace();
			if(null != session){
				try {
					session.setAutoCommit(true);//设回自动提交模式
					tran.rollbackTransaction();//回滚事务
				} catch (DbException e1) {
					e1.printStackTrace();
					throw new RuntimeException(e1.getMessage());
				}
			}
			throw new RuntimeException(e.getMessage());
		}finally{
			sessionManager.release();//需要关闭会话
		}
	}
	
	public boolean executeUpdates(ArrayList<String> al, SQLParameter param){
		if(null == al && al.size() == 0){
			return false;
		}
		try {
			this.getConnection();
			session.setAutoCommit(false);//不自动提交模式
			tran = session.createTransaction();//开始事务
			for(int i = 0; i < al.size(); i++){
				session.addBatch(al.get(i),param);
			}
			session.executeBatch();
			tran.commitTransaction();//提交事务
			session.setAutoCommit(true);//设回自动提交模式
			return true;
		} catch (DbException e) {
			e.printStackTrace();
			if(null != session){
				try {
					session.setAutoCommit(true);//设回自动提交模式
					tran.rollbackTransaction();//回滚事务
				} catch (DbException e1) {
					e1.printStackTrace();
					throw new RuntimeException(e.getMessage());
				}
			}
			throw new RuntimeException(e.getMessage());
		}finally{
			sessionManager.release();//需要关闭会话
		}
	}
}
 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值