在jsp页面中利用checkbox批量删除数据库中的记录

<%@ page language="java" import="java.util.*,java.sql.*,com.it.bean.*" pageEncoding="GBK"%>
<jsp:useBean id="common" class="com.it.bean.pageinfo" scope="page"/>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>

<script language="javascript" type="text/javascript"> 
var   flag=1; 
function   selectAll(){ 
if(flag==1){ 
if(common_delete.selectdelete.length!=undefined){ 
for   (var   i=0;i <common_delete.selectdelete.length;i++) 
common_delete.selectdelete[i].checked=true; 
}else{ 
common_delete.selectdelete.checked=true; 

flag=0; 
}else{ 
if(common_delete.selectdelete.length!=undefined){ 
for   (var   i=0;i <common_delete.selectdelete.length;i++) 
common_delete.selectdelete[i].checked=false; 
}else{ 
common_delete.selectdelete.checked=false; 

flag=1; 


     </script> 
  </head>
  <body>
  <div align="center">
  <div align="center">
  <form name="common_delete" method="post" action="../CommonDeleteServlet">
    <table bgcolor="#FFFFFF" border="0" cellspacing="0" cellpadding="5">
    <tr bgcolor="#cc99cc">
    <td width="10%" align="center"><input type= "button" name= "selall " value= "全选/反选 " onClick= "selectAll();"></td>
    <td width="10%" align="center"><font face="华文隶书" size="4">编号</font></td>
    <td width="10%" align="center"><font face="华文隶书" size="4">描述</font></td>
    <td width="10%" align="center"><font face="华文隶书" size="4">所属类</font></td>
    <td width="20%" align="center"><font face="华文隶书" size="4">诊断</font></td>
    <td width="10%" align="center"><font face="华文隶书" size="4">频率</font></td>
    </tr>
    <%
    int i=0;
    String setcolor;
   try
     {
        ResultSet rst1=common.getCommon("100");
        while(rst1.next())
          {
        if(i%2==0)
    setcolor="#ffffff";
    else
    setcolor="#D9D9D9";
    %>
    <tr bgcolor=<%=setcolor%>>
  <td width="10%" align="center"><input type="checkbox" name="selectdelete" value="<%=rst1.getString(1).trim()%>"></td>
  <td width="10%" align="center"><font face="楷体_GB2312" size="3"><%=rst1.getString(1).trim()%></font></td>
  <td width="10%" align="center"><font face="楷体_GB2312" size="3"><%=rst1.getString(3).trim()%></font></td>
  <td width="10%" align="center"><font face="楷体_GB2312" size="3"><%=rst1.getString(2).trim()%></font></td>
  <td width="20%" align="center"><font face="楷体_GB2312" size="3"><%=rst1.getString(4).trim()%></font></td>
  <td width="10%" align="center"><font face="楷体_GB2312" size="3"><%=rst1.getInt(6)%></font></td>
   </tr>
<%
   i++;
          }
        rst1.close();
      }
      catch(Exception e){}
    %>
    <tr><td colspan="3">&nbsp;</td> 
    <td align="center"> 
    <input type="submit" value="删    除" onclick="return confirm('你确定所选?')"></td></tr> 
    </table>
    </form>
    </div>
   </div>
  </body>
</html>


处理页:


public class CommonDeleteServlet extends HttpServlet {


/**

*/
private static final long serialVersionUID = 1L;


/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
PrintWriter out=response.getWriter();
String[] selectdelete = request.getParameterValues("selectdelete"); 
String ids =""; 
for(int i=0;i<selectdelete.length;i++){ 
ids += "'"+selectdelete[i]+"'"; 
if(i!=selectdelete.length-1) ids+=","; 
}
String sql = "delete from COMMON where id in ("+ids+")"; 
Connection con = null; 
Statement dodel = null;
DBcon dbcon = new DBcon();
try
{
con = dbcon.getConn();
dodel = con.createStatement();
dodel.executeUpdate(sql);
    con.close();
 }
catch(Exception e)
{
e.printStackTrace();
}
out.print("<script language='javascript'>alert('Do Success');window.location.href='delete.jsp';</script>");
out.flush();
out.close();

}


/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}


}

你可能感兴趣的:(在jsp页面中利用checkbox批量删除数据库中的记录)