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();//需要关闭会话
}
}
}