Jdbc 增删改查工具方法

/**
* 查询数据列表
* @param sql 查询SQL语句
* @param queryColum 要查询的字段
* @param type 查询类型 当type = "count" 为查询总条数
* @throws SQLException
*/
public static List<Map<String,Object>>  query(String sql,String queryColum,String type,String timeColum) throws SQLException{
Connection conn = DbUtils.getConnection();
PreparedStatement st = conn.prepareStatement(sql);
List<Map<String,Object>> list  = new ArrayList<Map<String,Object>>();
ResultSet result = st.executeQuery(sql);
if("count".equals(type)){//查询总条数
            while(result.next()){
            Map<String,Object> map = new HashMap<String, Object>();
String  total = result.getString("total");
map.put("total", total);
list.add(map);
}
}
if(null !=queryColum && !"".equals(queryColum)){
while(result.next()){

Map<String,Object> map = new HashMap<String, Object>();
String[] clounName = queryColum.split(",");

for (String s : clounName) {
if(s.trim().equals(timeColum)){
long ll = result.getLong(s);
map.put(s, Tools.transLong2Date(ll));
}else{
map.put(s, result.getString(s).replaceAll("<", "").replaceAll(">", ""));
}

}
list.add(map);
}
}
result.close();
st.close();
conn.close();
return list;
}


/**
* 更新
* @param string
* @param type 当type 为del是不拆分
* @param updateColumnValue
* @throws SQLException 
*/
public static int  update(String sql, String updateColumn,String type) throws SQLException {
Connection conn = DBAccesser.getMachineConnection();
PreparedStatement ps = null;
int status = 0;
if(null != updateColumn && !"".equals(updateColumn)){
ps =  conn.prepareStatement(sql);
if(!"del".equals(type)){
String[] array = updateColumn.split(",");
    for (int i = 0; i < array.length; i++) {
ps.setString(i+1, array[i]);
}
}else{
ps.setString(1, updateColumn);
}

    status = ps.executeUpdate();
}
ps.close();
conn.close();
return status;
}


public static int update2(String sql) throws SQLException{
Connection conn = DBAccesser.getMachineConnection();
Statement ps = null;
int i =0;
    try {
      ps = conn.createStatement();
          i = ps.executeUpdate(sql);
        ps.close();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return i;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值