www.pudn.com > dbPool.rar > DBExecutor.java


package dev.trade.common.db; 
 
import org.apache.log4j.*; 
import java.sql.*; 
import java.util.*; 
 
/** 
 * 

Title: 数据库常用操作执行类,整合了一些常用的操作

* *

Description:

* *

Copyright: Copyright (c) 2008

* *

Company:

* * @author Lucas * @version 1.0 */ public class DBExecutor { private static Logger log = Logger.getLogger(DBExecutor.class); public DBExecutor(){ } public static boolean isEmpty(String str){ return str == null || str.trim().length() == 0; } /** * 使用默认连接池中的连接完成数据插入操作 * @param tableName String 要插入的表名 * @param values Map 数据(名=字段名,值=要插入的值) * @return int 实际插入的行数 * @throws Exception */ public static int insertTable(String tableName, Map values) throws Exception{ return insertTable(DBUtils.DEF_POOL_NAME, tableName, values); } /** * 使用指定连接池中的连接完成数据插入操作 * @param poolName String * @param tableName String 要插入的表名 * @param values Map 数据(名=字段名,值=要插入的值) * @return int 实际插入的行数 * @throws Exception */ public static int insertTable(String poolName, String tableName, Map values) throws Exception{ if(isEmpty(tableName) || values == null || values.size() == 0) throw new Exception("表名或值列表为空,操作无法完成"); Connection conn = null; try{ conn = DBUtils.getDBConn(poolName); return insertTable(conn, tableName, values); }finally{ DBUtils.returnDBConn(poolName, conn); } } /** * 使用指定的连接完成数据的插入操作 * @param conn Connection 指定的连接 * @param tableName String 要插入的表名 * @param values Map 数据(名=字段名,值=要插入的值) * @return int 实际插入的行数 * @throws Exception */ public static int insertTable(Connection conn, String tableName, Map values) throws Exception{ if(isEmpty(tableName) || values == null || values.size() == 0) throw new Exception("表名或值列表为空,操作无法完成"); PreparedStatement ps = null; try{ StringBuffer sbSql = new StringBuffer("insert into "); sbSql.append(tableName).append(" ("); StringBuffer sbVal = new StringBuffer("values("); List paramList = new ArrayList(); Object key = null, val = null; Iterator it = values.keySet().iterator(); for(int n = 0; it.hasNext(); n++){ key = it.next(); val = values.get(key); if(n > 0){ sbSql.append(","); sbVal.append(","); } sbSql.append(key); if(val == null){ sbVal.append("null"); } else if(val instanceof DBConstant){ sbVal.append(val.toString()); } else{ sbVal.append("?"); paramList.add(val); } } sbSql.append(") ").append(sbVal).append(")"); ps = conn.prepareStatement(sbSql.toString()); for(int i=0, l = paramList.size(); i < l; i++) ps.setObject(i + 1, paramList.get(i)); return ps.executeUpdate(); } catch(Exception ex){ log.error("执行表:" + tableName + "的数据插入时发生错误,值=" + values, ex); throw ex; } finally{ DBUtils.closeStmt(ps); } } /** * 使用默认连接池中的连接完成数据的更新操作 * @param tableName String 要更新的表名 * @param values Map 要更新的值(名=字段名,值=更新值) * @param condition String 更新记录的过滤条件(Where后的子名) * @return int 实际更新的记录数 * @throws Exception */ public static int updateTable(String tableName, Map values, String condition) throws Exception{ return updateTable(DBUtils.DEF_POOL_NAME, tableName, values, condition); } /** * 使用指定连接池中的连接完成数据的更新操作 * @param poolName String 指定连接池名称 * @param tableName String 要更新的表名 * @param values Map 要更新的值(名=字段名,值=更新值) * @param condition String 更新记录的过滤条件(Where后的子名) * @return int 实际更新的记录数 * @throws Exception */ public static int updateTable(String poolName, String tableName, Map values, String condition) throws Exception{ if(isEmpty(tableName) || values == null || values.size() == 0) throw new Exception("表名或值列表为空,操作无法完成"); Connection conn = null; try{ conn = DBUtils.getDBConn(poolName); return updateTable(conn, tableName, values, condition); }finally{ DBUtils.returnDBConn(poolName, conn); } } /** * 使用指定的连接完成数据更新操作 * @param conn Connection 指定的连接 * @param tableName String 要更新的表名 * @param values Map 要更新的值(名=字段名,值=更新值) * @param condition String 更新记录的过滤条件(Where后的子名) * @return int 实际更新的记录数 * @throws Exception */ public static int updateTable(Connection conn, String tableName, Map values, String condition) throws Exception{ if(isEmpty(tableName) || values == null || values.size() == 0) throw new Exception("表名或值列表为空,无法完成操作"); PreparedStatement ps = null; try{ StringBuffer sbSql = new StringBuffer("update "); sbSql.append(tableName).append(" set "); List paramList = new ArrayList(); Object key = null, val = null; Iterator it = values.keySet().iterator(); for(int n = 0; it.hasNext(); n++){ key = it.next(); val = values.get(key); if(n > 0) sbSql.append(","); if(val == null){ sbSql.append(key).append("=null"); }else if(val instanceof DBConstant){ sbSql.append(key).append("=").append(val.toString()); } else{ sbSql.append(key).append("=?"); paramList.add(val); } } if(!isEmpty(condition)) sbSql.append(" where ").append(condition); ps = conn.prepareStatement(sbSql.toString()); for(int i=0, l = paramList.size(); i < l; i++) ps.setObject(i + 1, paramList.get(i)); return ps.executeUpdate(); } catch(Exception ex){ log.error("执行表:" + tableName + "的数据更新操作时发生错误,值=" + values + ",条件=" + condition, ex); throw ex; } finally{ DBUtils.closeStmt(ps); } } /** * 使用默认连接池中的连接完成数据删除操作 * @param tableName String 要删除的数据所在表名 * @param condition String 删除数据的过滤条件 * @return int 实际删除的记录数 * @throws Exception */ public static int deleteTable(String tableName, String condition) throws Exception{ return deleteTable(DBUtils.DEF_POOL_NAME, tableName, condition); } /** * 使用指定连接池中的连接完成数据删除操作 * @param poolName String 指定的连接池名称 * @param tableName String 要删除的数据所在表名 * @param condition String 删除数据的过滤条件 * @return int 实际删除的记录数 * @throws Exception */ public static int deleteTable(String poolName, String tableName, String condition) throws Exception{ if(isEmpty(tableName)) throw new Exception("表名为空,无法完成操作"); Connection conn = null; try{ conn = DBUtils.getDBConn(poolName); return deleteTable(conn, tableName, condition); }finally{ DBUtils.returnDBConn(poolName, conn); } } /** * 使用指定的连接完成数据删除操作 * @param conn Connection 指定的连接 * @param tableName String 要删除的数据所在表名 * @param condition String 删除数据的过滤条件 * @return int 实际删除的记录数 * @throws Exception */ public static int deleteTable(Connection conn, String tableName, String condition) throws Exception{ if(isEmpty(tableName)) throw new Exception("表名为空,无法完成操作"); PreparedStatement ps = null; try{ StringBuffer sbSql = new StringBuffer("delete from "); sbSql.append(tableName); if(!isEmpty(condition)) sbSql.append(" where ").append(condition); ps = conn.prepareStatement(sbSql.toString()); return ps.executeUpdate(); } catch(Exception ex){ log.error("执行表:" + tableName + "的数据删除操作发生错误,条件=" + condition, ex); throw ex; } finally{ DBUtils.closeStmt(ps); } } /** * 使用默认连接池中的连接删除表中的数据 * @param tableName String 要删除的表名 * @param values Map 过滤条件(名=字段名, 值=条件值) * @param isOrJoin boolean 是否使用OR连接各个条件(默认用AND) * @return int 实际删除的记录数 * @throws Exception */ public static int deleteTable(String tableName, Map values, boolean isOrJoin) throws Exception{ return deleteTable(DBUtils.DEF_POOL_NAME, tableName, values, isOrJoin); } /** * 使用指定连接池中的连接删除表中的数据 * @param poolName String 连接池名称 * @param tableName String 要删除的表名 * @param values Map 过滤条件(名=字段名, 值=条件值) * @param isOrJoin boolean 是否使用OR连接各个条件(默认用AND) * @return int 实际删除的记录数 * @throws Exception */ public static int deleteTable(String poolName, String tableName, Map values, boolean isOrJoin) throws Exception{ if(isEmpty(tableName)) throw new Exception("表名为空,无法完成操作"); Connection conn = null; try{ conn = DBUtils.getDBConn(poolName); return deleteTable(conn, tableName, values, isOrJoin); }finally{ DBUtils.returnDBConn(poolName, conn); } } /** * 使用指定的连接删除表中的数据 * @param conn Connection 指定的连接 * @param tableName String 要删除的表名 * @param values Map 过滤条件(名=字段名, 值=条件值) * @param isOrJoin boolean 是否使用OR连接各个条件(默认用AND) * @return int 实际删除的记录数 * @throws Exception */ public static int deleteTable(Connection conn, String tableName, Map values, boolean isOrJoin) throws Exception{ if(isEmpty(tableName)) throw new Exception("表名为空,无法完成操作"); if(values == null || values.size() == 0) return deleteTable(conn, tableName, ((String)(null))); PreparedStatement ps = null; try{ StringBuffer sbSql = new StringBuffer("delete from "); sbSql.append(tableName).append(" where "); List paramList = new ArrayList(); Iterator it = values.keySet().iterator(); for(int n = 0; it.hasNext(); n++){ Object key = it.next(); Object val = values.get(key); if(n > 0) sbSql.append(isOrJoin ? " or " : " and "); if(val == null){ sbSql.append(key).append(" is null"); }else if(val instanceof DBConstant){ sbSql.append(key); if(val.equals(DBConstant.PARAM_IS_NULL) || val.equals(DBConstant.PARAM_IS_NOT_NULL)) sbSql.append(" "); else sbSql.append("="); sbSql.append(val.toString()); } else{ sbSql.append(key).append("=?"); paramList.add(val); } } ps = conn.prepareStatement(sbSql.toString()); for(int i=0,l = paramList.size(); i < l; i++) ps.setObject(i + 1, paramList.get(i)); return ps.executeUpdate(); } catch(Exception ex){ log.error("执行表:" + tableName + "的数据删除操作发生错误,条件="+values, ex); throw ex; } finally{ DBUtils.closeStmt(ps); } } /** * 使用默认连接池中的连接获取指定表的字段列表 * @param tableName String 表名 * @return List 字段列表 * @throws Exception */ public static List getTableColumns(String tableName) throws Exception{ return getTableColumns(DBUtils.DEF_POOL_NAME, tableName); } /** * 使用指定连接池中的连接获取表的字段列表 * @param poolName String 连接池名称 * @param tableName String 表名 * @return List 字段列表 * @throws Exception */ public static List getTableColumns(String poolName, String tableName) throws Exception{ if(isEmpty(tableName)) throw new Exception("表名为空,无法完成操作"); Connection conn = null; try{ conn = DBUtils.getDBConn(poolName); return getTableColumns(conn, tableName); }finally{ DBUtils.returnDBConn(poolName, conn); } } /** * 使用指定的连接获取表的字段列表 * @param conn Connection 指定的连接 * @param tableName String 表名 * @return List 字段列表 * @throws Exception */ public static List getTableColumns(Connection conn, String tableName) throws Exception{ if(isEmpty(tableName)) throw new Exception("表名为空,无法完成操作"); PreparedStatement ps = null; ResultSet rs = null; ResultSetMetaData meta = null; try{ String sql = "select * from " + tableName + " where 1=2"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); meta = rs.getMetaData(); int cnt = meta.getColumnCount(); List colList = new ArrayList(); for(int i = 1; i <= cnt; i++) colList.add(DBColumn.getDBColumn(meta, i)); return colList; } catch(Exception ex){ log.error("获取表:" + tableName + "的字段列表时发生错误", ex); throw ex; } finally{ DBUtils.closeRS(rs); DBUtils.closeStmt(ps); } } /** * 使用默认连接池中的连接执行查询 * @param sql String 要执行的查询语名 * @return DBDataBox * @throws Exception */ public static DBDataBox executeQuery(String sql) throws Exception{ return executeQuery(DBUtils.DEF_POOL_NAME, sql, null); } /** * 使用指定连接池中的连接执行查询 * @param poolName String 连接池名称 * @param sql String 查询SQL * @return DBDataBox * @throws Exception */ public static DBDataBox executeQuery(String poolName, String sql) throws Exception{ return executeQuery(poolName, sql, null); } /** * 使用指定的连接执行查询 * @param conn Connection 指定的连接 * @param sql String 查询SQL * @return DBDataBox * @throws Exception */ public static DBDataBox executeQuery(Connection conn, String sql) throws Exception{ return executeQuery(conn, sql, null); } /** * 使用默认连接池中的连接执行查询 * @param sql String SQL(带有?号的SQL) * @param paramList List 用于替换SQL中的?的值列表 * @return DBDataBox * @throws Exception */ public static DBDataBox executeQuery(String sql, List paramList) throws Exception{ return executeQuery(DBUtils.DEF_POOL_NAME, sql, paramList); } /** * 使用指定连接池中的连接执行查询 * @param poolName String 连接池名称 * @param sql String SQL(带有?号的SQL) * @param paramList List 用于替换SQL中的?的值列表 * @return DBDataBox * @throws Exception */ public static DBDataBox executeQuery(String poolName, String sql, List paramList) throws Exception{ if(isEmpty(sql)) throw new Exception("查询SQL为空,无法完成操作"); Connection conn = null; try{ conn = DBUtils.getDBConn(poolName); return executeQuery(conn, sql, paramList); }finally{ DBUtils.returnDBConn(poolName, conn); } } /** * 使用指定的连接执行查询 * @param conn Connection 指定的连接 * @param sql String SQL(带有?号的SQL) * @param paramList List 用于替换SQL中的?的值列表 * @return DBDataBox * @throws Exception */ public static DBDataBox executeQuery(Connection conn, String sql, List paramList) throws Exception{ if(isEmpty(sql)) throw new Exception("查询SQL为空,无法完成操作"); PreparedStatement ps = null; ResultSet rs = null; ResultSetMetaData meta = null; try{ ps = conn.prepareStatement(sql); int i = 0, j = 1; if(paramList != null && paramList.size() > 0){ Object obj = null; for(i = 0; i < paramList.size(); i++){ obj = paramList.get(i); if(obj instanceof DBConstant){ DBConstant dbConst = (DBConstant)obj; if(DBConstant.TAG_NULL.equals(dbConst.getTag())) ps.setNull(i + 1, dbConst.getType()); else ps.setObject(i + 1, dbConst.toString()); }else if(obj == null) ps.setNull(i + 1, Types.VARCHAR); else ps.setObject(i + 1, obj); } } rs = ps.executeQuery(); meta = rs.getMetaData(); int cnt = meta.getColumnCount(); DBDataBox box = new DBDataBox(cnt); for(i = 1; i <= cnt; i++) box.addColumn(DBColumn.getDBColumn(meta, i)); for(i = 0; rs.next(); i++) for(j = 1; j <= cnt; j++) box.addData(i, rs.getObject(j)); return box; } catch(Exception ex){ log.error("执行查询操作时发生错误,SQL=" + sql + ",值列表=" + paramList, ex); throw ex; } finally{ DBUtils.closeRS(rs); DBUtils.closeStmt(ps); } } /** * 使用默认连接池中的连接执行更新 * @param sql String 更新SQL * @return int 实际更新的记录数 * @throws Exception */ public static int executeUpdate(String sql) throws Exception{ return executeUpdate(DBUtils.DEF_POOL_NAME, sql, null); } /** * 使用指定连接池中的连接执行更新 * @param poolName String 连接池名称 * @param sql String 更新SQL * @return int 实际更新的记录数 * @throws Exception */ public static int executeUpdate(String poolName, String sql) throws Exception{ return executeUpdate(poolName, sql, null); } /** * 使用指定连接池中的连接执行更新 * @param sql String SQL(带有?号的SQL) * @param paramList List 用于替换SQL中的?的值列表 * @return int 实际更新的记录数 * @throws Exception */ public static int executeUpdate(String sql, List paramList) throws Exception{ return executeUpdate(DBUtils.DEF_POOL_NAME, sql, paramList); } /** * 使用指定连接池中的连接执行更新 * @param poolName String 连接池名称 * @param sql String SQL(带有?号的SQL) * @param paramList List 用于替换SQL中的?的值列表 * @return int 实际更新的记录数 * @throws Exception */ public static int executeUpdate(String poolName, String sql, List paramList) throws Exception{ if(isEmpty(sql)) throw new Exception("SQL为空,无法完成操作"); Connection conn = null; try{ conn = DBUtils.getDBConn(poolName); return executeUpdate(conn, sql, paramList); }finally{ DBUtils.returnDBConn(poolName, conn); } } /** * 使用指定的连接执行更新 * @param conn Connection 指定的连接 * @param sql String SQL(带有?号的SQL) * @param paramList List 用于替换SQL中的?的值列表 * @return int 实际更新的记录数 * @throws Exception */ public static int executeUpdate(Connection conn, String sql, List paramList) throws Exception{ if(isEmpty(sql)) throw new Exception("SQL为空,无法完成操作"); PreparedStatement ps = null; try{ ps = conn.prepareStatement(sql); if(paramList != null && paramList.size() > 0){ Object obj = null; for(int i = 0; i < paramList.size(); i++){ obj = paramList.get(i); if(obj instanceof DBConstant){ DBConstant dbConst = (DBConstant)obj; if(DBConstant.TAG_NULL.equals(dbConst.getTag())) ps.setNull(i + 1, dbConst.getType()); else ps.setObject(i + 1, dbConst.toString()); }else if(obj == null) ps.setNull(i + 1, Types.VARCHAR); else ps.setObject(i + 1, obj); } } return ps.executeUpdate(); } catch(Exception ex){ log.error("执行更新时发生错误,SQL=" + sql + ",值=" + paramList, ex); throw ex; } finally{ DBUtils.closeStmt(ps); } } }