1、在jsp页中放置一个checkbox控件
<input type="checkbox" name="sid" value=${student.stud_id } />//该checkbox放在循环体中,其中value必须获取主键的值。这样就可以有多个name值相同,value值不同的checkbox控件。提交时传过来的是字符串数组。
2、接收的Servlet中进行处理
private void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("sid") != null) {
String[] sid = request.getParameterValues("sid");//字符串数组接收传入的参数sid
//将数组中的字符串拼接成一个字符串,各字符串之间用","隔开
String stud_id = "";
for (int i = 0; i < sid.length; i++) {
stud_id += "‘" + sid[i] + "‘,";
}
stud_id = stud_id.substring(0, stud_id.length() - 1);
StudentManage sManage = new StudentManage();
if (sManage.RemoveStudent(stud_id) > 0) {
Common.ShowMessage("删除成功!");
request.getRequestDispatcher("StudentList.jsp").forward(
request, response);
}
}
}
3.sql删除语句
// 删除学生
public int RemoveStudent(String stud_id) {
DbAccess dbAccess = new DbAccess();
String sql = "delete from student where stud_id in(" + stud_id + ")";
int ret= dbAccess.excuteUpdate(sql);
dbAccess.close();
return ret;
}