//采用Statement 拼串法批量删除
public void delUser(String[] userIds){
StringBuilder stbStr = new StringBuilder();
for(int i=0;i<userIds.length;i++){
stbStr.append("'")
.append(userIds[i])
.append("'")
.append(",");
}
String sql = "delete from t_user where user_id in (" + stbStr.substring(0, stbStr.length() -1)+ ")";
System.out.println("UserManager.delUser() -->" +sql);
Connection conn = null;
Statement stmt = null;
try{
conn = DbUtil.getConnection();
stmt = conn.createStatement();
stmt.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}finally{
DbUtil.close(stmt);
DbUtil.close(conn);
}
}
// PreparedStatement 占位符批量删除
public void delUser(String[] userIds){
StringBuilder stbStr = new StringBuilder();
for(int i=0;i<userIds.length;i++){
stbStr.append("?");
if(i<(userIds.length - 1)){
stbStr.append(",");
}
}
String sql = "delete from t_user where user_id in (" + stbStr.toString() +")";
System.out.println("UserManager.delUser() -->" +sql);
Connection conn = null;
PreparedStatement stmt = null;
try{
conn = DbUtil.getConnection();
stmt = conn.prepareStatement(sql);
for(int i=0;i<userIds.length;i++){
stmt.setString(i + 1, userIds[i]);
}
stmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
DbUtil.close(stmt);
DbUtil.close(conn);
}
}