看了挺多的回答,但没弄明白。他们说批量删除需要NAME属性相同,但是name是字符数组型的吗?选择后提交出去是什么形式的?怎么可能用request.getParameter("productId");多次取得,并在数据库里执行多个删除?还有个回答是说"delete   news   where   id   in   ("   &   deleteid   &   ")"但是写进Deleteservlet后编译不出来。麻烦帮我改改,实在找不到答案。谢谢了
************************
deleteProduct.jsp
*********************************************************************
<form action="/wy/servlet/DeteleServlet" method="POST">
<table width=80% border="1" bgcolor="#0099CC">
<tr bgcolor="#009966" bordercolor="#990066">
<td>id</td><td>类别</td><td>名称</td><td>出厂商</td><td>价格</td><td>描述</td><td>删除</td>
<%
request.setCharacterEncoding("gb2312");
response.setContentType("text/html; charset=gb2312");
Collection products=product.getAllProduct();
Iterator it=products.iterator();
while(it.hasNext()){
Product temp=(Product)it.next();
out.println("<tr><td>"+temp.getProductId()+"</td>");
out.println("<td>"+temp.getCategoryId()+"</td>");
try
{
out.println("<td>"+trans(temp.getName())+"</td>");
out.println("<td>"+trans(temp.getProducer())+"</td>");
out.println("<td>"+temp.getPrice()+"</td>");
out.println("<td>"+trans(temp.getDescription())+"</td>");
out.println("<td><input type=\"Checkbox\" name="+temp.getProductId()+" value="+temp.getProductId()+"></td>");
}
catch(Exception e){}}
%><br><tr><td><input type="submit" value="提交"></td></tr>
</table>
</form>
*********************************************************************
DeteleServlet.java
************************
package myservlet; 
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.sql.*;
import com.jspdev.ch12.*;
import com.jspdev.util.*;
public class DeteleServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter(); String productId=request.getParameter("productId");
try{
 //下面为写入数据库
 ProductBean aduB=new ProductBean();
 aduB.deleteProduct(productId);
 out.println("<html>");
         out.println("<head><title>addRecord</title></head>");
         out.println("<body>");
         out.println("<p>Add Success</p>");
 out.println("<a href=\"/wy/index.html\">return</a>");
         out.println("</body></html>");
}catch(Exception e){
e.printStackTrace();
}
}
public void destroy() {
super.destroy(); 
}
}
*********************************************************************
ProductBean.java
************************
package com.jspdev.ch12;import java.sql.*;
import java.util.*;
import java.io.*;
import com.jspdev.util.*;
/**
 *ProductBean包含和Product表相关的操作
 */
public class ProductBean
{
private Connection con;
//构造方法,获得数据库的连接。
public ProductBean()
{
this.con=DataBaseConnection.getConnection();
}
public void deleteProduct(String productId)throws Exception
{
PreparedStatement wstmt=con.prepareStatement("delete from products where productid=?");
wstmt.setString(1,productId);
wstmt.execute();
}
}
*********************************************************************
************************
*********************************************************************
************************
*********************************************************************

解决方案 »

  1.   

    今天刚好做了个这样的程序,只不过我是UPDATE,不是DELETE, 原理是一样的
    页面里的CHECKBOX名字要带上序号
    比如我是用程序循环生成的
    <%for(int i;i<结果集行数;i++){%>
     <input type="checkbox" value="chk" name="chk<%=每行的主键%>" />
    <%}%>然后在SERVLET里取值的时候
    先把结果集取到,因为要用主键
    ArrayList ltChk=new ArrayList();
    for(int i=0;i<结果集行数;i++){
    if(util.nullSafe(request.getParameter("chk"+主键)).equals("chk"))//循环所有的CHECKBOX
    {
    ltChk.add(主键);
    }
    }
    这是判断是否选中, 如果选中,就把主键加到一个ArrayList里,
    然后在拼SQL的时候加上WHERE条件时, 从ArrayList取主键
    StringBuffer strbuf = new StringBuffer();
    strbuf.append(" UPDATE 表名 ");
    strbuf.append(" SET 字段=值 ");
    strbuf.append(" WHERE 主键 IN(");
    for(int intIndex=0;intIndex<ltChk.size();intIndex++)
    {
    strbuf.append(ltChk.get(intIndex).toString());
    if(intIndex!=ltChk.size()-1)
    {
    strbuf.append(",");
    }
    }
    strbuf.append(") ");然后String strSQL=strbuf.toString();
    就可以执行strSQL这个SQL了.
    程序完全能运行,看不懂的话加MSN  [email protected]
    我给你发完整程序,不过是日文的