📄 dbexecutor.java.svn-base
字号:
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<DBColumn> 字段列表
* @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<DBColumn> 字段列表
* @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<DBColumn> 字段列表
* @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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -