servlet中的多重删除

时间:2016-02-01 06:14:06

标签: java servlets

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath());

 String[] id = request.getParameterValues("hiddenID");
 //int id=Integer.parseInt(request.getParameter("hiddenID"));

 foodDB db = new foodDB();
 Boolean success = db.deletefood(id);

 //Boolean success=(Boolean)db.deletefood("%" + id + "%");
 //boolean success=(Boolean)db.deletefood( F );  

 request.setAttribute("success", success);

 RequestDispatcher rd = request.getRequestDispatcher("delete.jsp");
 rd.forward(request, response);

}

这是我的servlet部分。使用选中的复选框在数据库中多次删除。但它不起作用。

1 个答案:

答案 0 :(得分:0)

这句话错了

 pstmt.setString(1, "%" + id + "%");

此处id是一个字符串数组。所以你将字符串数组对象传递给setString()。但不是数组值。

使用for-each循环从数组id中获取每个id。

PreparedStatement pstmt=null;

for(String i:id)
{
     pstmt=conn.prepareStatement(sqlStr);
     pstmt.setString(1, "%" + i + "%");
     //now execute it

}