⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbexecutor.java.svn-base

📁 自用的一个简单的数据库连接池
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
package dev.trade.common.db;

import org.apache.log4j.*;
import java.sql.*;
import java.util.*;

/**
 * <p>Title: 数据库常用操作执行类,整合了一些常用的操作</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: </p>
 *
 * @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++){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -