jsp中对数据进行批量删除操作

批量删除的SQL:delete from user where uid in(主键列表);
UserBiz:
//批量删除
public boolean batchDelete(String[] uids);
UserBizImpl:

public boolean batchDelete(String[] uids) {
        String sql = "delete from user where uid in(";
        for(String uid:uids){
            sql+=uid+",";
        }
        //去掉最后一个逗号,增加一个)
        sql = sql.substring(0, sql.length()-1);
        sql+=")";
        Object[] params={};//没有参数
        return udao.update(sql, params);
    }

UserServlet:

        HttpServletResponse response) throws ServletException, IOException{
        //获取批量编号(注意,复选框的name值为chk_uid)
        String[] uids = request.getParameterValues("chk_uid");
        boolean result = ubiz.batchDelete(uids);

        if(result == true){
            //批量成功
            request.setAttribute("msg", "操作成功");
        }else{
            request.setAttribute("msg", "操作失败");
        }
        findAllUser(request,response);
    }

userlist.jsp
全选操作

//全选或清空
  function setChecked(boxname) {
    var boxs =document.getElementsByName(boxname);
    var chkbox = document.getElementById("chkall");
    if(chkbox.checked == true){
        for( i=0 ; ichecked = true;
        }
    }else{
        for( i=0 ; ichecked = false;
        }
    }
}

批量删除按钮

表单增加onSubmit事件

function checkForm(){
    var boxs =document.getElementsByName("chk_uid");
    for( i=0 ; iif(boxs[i].checked==true){
            if(confirm("确定批量删除这些数据吗?")){
                return true;
            }else{
                return false;
            }
        }
    }
    alert("至少选中一条数据……");
    return false;
}

你可能感兴趣的:(jsp,jsp)