批量删除
相关测试用例:
如果id为整数
public class DelTest {
@Test
public void test1(){
String[] ids={"1","2","4"};
//(1,2,3)
//第一步把ids转成字符串
System.out.println(Arrays.toString(ids).replace("[", "(").replace("]", ")"));
}
}
如果id为字符串
public static void main(String[] args) {
//String[] ids ---->('id1','id2','id3')
String[] ids={"id1","id2","id3"};
StringBuffer ids_str = new StringBuffer();
for(int i=0;i<ids.length;i++){
ids_str.append("\'");
ids_str.append(ids[i]);
ids_str.append("\',");
}
String sql = "delete from tab_user where id in(" + ids_str.substring(0, ids_str.length()-1) + ")";
System.out.println(sql);
}
jsp页form
<form action="${pageContext.request.contextPath }/delServlet">
<input type="checkbox" name="chkall" id="chkall"
onclick="selectAll(this)" />全选
<table width="100%" border="1" align="center" bordercolor="#dadada">
<tr align="center">
<td width="44" height="25" valign="middle">选择</td>
<td width="98" height="25" valign="middle">账号</td>
<td width="132" height="25" valign="middle">email</td>
<td width="132" height="25" valign="middle">身份证号</td>
<td width="132" height="25" valign="middle">权限</td>
<td width="132" height="25" valign="middle">操作</td>
</tr>
<!-- 循环显示每个用户的信息 用jstl-->
<c:forEach items="${userList}" var="user" varStatus="status">
<tr align="center">
<td width="44" height="25" valign="middle"><input
type="checkbox" name="chkone" value="${user.id }"
onclick="chkOne(this)" /></td>
<td width="98" height="25" valign="middle">${user.userName }</td>
<td width="132" height="25" valign="middle">${user.email }</td>
<td width="132" height="25" valign="middle">${user.idCard }</td>
<td width="132" height="25" valign="middle">${user.power }</td>
<td width="132" height="25" valign="middle"><a href="#">修改</a></td>
</tr>
</c:forEach>
<input type="button" value="删除" onclick="del();">
</form>
js
function del() {
var result = confirm("您确定要删除吗?");
if (result == true) {
document.forms[0].submit();
}
}
servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//第一步 获取页面选中的ID
String[] ids=request.getParameterValues("chkone");
//至少选中一个
if(ids!=null&&ids.length>0){
//调用删除方法执行删除
IUserService service=new UserServiceImpl();
int count=service.delete(ids);
if(count>0) {
//删除成功
request.setAttribute("resultMsg", "操作成功");
}
}else{
//给客户端一个响应
request.setAttribute("resultMsg", "操作失败");
}
request.getRequestDispatcher("/findAllUserServlet").forward(request, response);
}
service略
dao --id为整型
@Override
public int delete(String[] ids) {
// delete from t where id in(1,2,3);
int count=0;
conn=DBUtil.getConnection();
try {
StringBuffer sql=new StringBuffer("delete from t_user where user_id in ");
sql.append(Arrays.toString(ids).replace("[", "(").replace("]", ")"));
ps=conn.prepareStatement(sql.toString());
count=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
DBUtil.closeConnection(rs, ps, conn);
return count;
}
dao --id为字符串型
public int delAll(String[] ids) {
int j=0;
StringBuffer ids_str = new StringBuffer();
for(int i=0;i<ids.length;i++){
ids_str.append("\'");
ids_str.append(ids[i]);
ids_str.append("\',");
}
String sql = "delete from tab_user where id in(" + ids_str.substring(0, ids_str.length()-1) + ")";
//获取连接
Connection conn = DBUtils.getConnection();
PreparedStatement pst = null;
try {
pst=conn.prepareStatement(sql);
//执行语句(查询有结果集 (insert update delete---成功的条数))
j=pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return j;
}