我正在尝试使用复选框从我的sql数据库中删除多行。这是我的代码。我只能逐行删除没有复选框。寻求帮助,希望这里的任何人都可以指导我。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet_2.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="validation.js"> </script>
<title>All Food Menu</title>
</head>
<body>
<!--############# HEADER ############# -->
<table id="t1Body_food">
<tr><td colspan="5">
<table class="table_header" align="center" >
<tr> <td class="tbody-logo_food" align="right" valign="bottom">
<form name="myForm" action="searchFood.jsp" onsubmit="return searchvalidate()" method="post">
<input type="text" name="type" placeholder="Search">
<input name="search" type="submit" value="search">
<input type="button" value="Logout" onclick="logoutpage()"/>
</form></td></tr>
</table> </td> </tr>
<tr>
<td class="td1-header"id="rice" > <a href="main.html" class="link-header"> Home </a></td>
<td class="td1-header"> <a href="allFood.jsp" class="link-header"> Food </a></td>
<td class="td1-header"> <a href="reservation.html" class="link-header"> Reservation </a></td>
<td class="td1-header"> <a href="about_us.html" class="link-header"> About Us </a></td>
<td class="td1-header"> <a href="contact_us.html" class="link-header"> Contact us </a></td>
</tr>
</table>
<table id="t2Body" >
<tr><td colspan="5" id="rice"><hr color="orange" size="13"></td>
</tr>
<tr> <td class="td_2header" > Name</td>
<!-- <td class="td_2header" > Type</td>
<td class="td_2header" > Picture </td> -->
<td class="td_2header" > Description </td>
<td class="td_2header" > Price </td>
<td align="right" colspan="4"> <a target="_blank" href="addItem_Form.jsp">
<button name="AddItem" > +Add New Food Menu </button> </a>
</td>
</tr>
<tr><td colspan="5" id="rice"><hr color="orange" size="13"></td>
</tr>
<%
String id= request.getParameter("hiddenID");
try {
// Step1: Load JDBC Driver
Class.forName("com.mysql.jdbc.Driver");
// Step 2: Define Connection URL
String connURL ="jdbc:mysql://localhost/restaurant?user=root& password=Password";
// Step 3: Establish connection to URL
Connection conn = DriverManager.getConnection(connURL);
// Step 4: Create Statement object
Statement stmt = conn.createStatement();
// Step 5: Execute SQL Command
PreparedStatement pstmt =conn.prepareStatement("Select * from food");
ResultSet rs=pstmt.executeQuery();
// out.println("<table id='t2Body' >");
while (rs.next())
{ id=rs.getString("ID");
String name=rs.getString("name");
String type=rs.getString("type");
String pic=rs.getString("picture");
int price=rs.getInt("price");
String desc=rs.getString("description");
// out.println("<tr> <td class=td_2border><div class=food_name>"+id+" </div>");
out.println(" <tr>s<td class=td_2border><div class=food_name>"+name+"</div>");
out.println(" <div><img src=\" " + pic+"\" "+"/></div>");
out.println("<div>"+type+"</div><br>");
out.println("<div><hr color=orange></div> ");
out.println("</td>");
out.println("<td class=td2_desc><br>"+ desc+"</td>");
out.println("<td class=td_2border><br> $"+price+"</td>");
%>
<td class=td_2border>
<br>
<form action="delete.jsp" method="get">
<input type="checkbox" name="hiddenID" value="<%=id%>">
<input type="hidden" name="hiddenID" value="<%=id%>">
<input type="submit" Value="Delete" name ="delete"/>
<%-- <input type="submit" value="<%=id %>" name="check" /> --%>
</form>
</td>
<td class=td_2border><br>
<form action="update_Form.jsp" method="get">
<input type="hidden" name="hiddenID" value="<%=id%>" />
<input type="submit" value="Update" name="updateBtn" />
</form>
</td>
</tr>
<%
}
%>
<tr>
<td colspan="5" align="right">
<form action="delete.jsp" method="get">
<%-- <input type="hidden" name="hiddenID" value="<%=id%>" > --%>
<input type="submit" name="submit" value="confirm" >
</form>
</td>
</tr>
</table>
<%
}
catch(Exception e ){
out.println(e);}
%>
</body>
</html>
<%@page import="java.sql.*"%>
<%
String [] id = request.getParameterValues("hiddenID");
// String id= request.getParameter("hiddenID");
if (id != null && id.length != 0) {
out.println("You have selected: ");
for (int i = 0; i < id.length; i++) {
out.println(id[i]);
} //end for loop
}// end if
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// Step 2: Define Connection URL
String connURL = "jdbc:mysql://localhost/restaurant?user=root&password=Password";
// Step 3: Establish connection to URL
conn = DriverManager.getConnection(connURL);
String sqlStr = "Delete from food WHERE id =?";
PreparedStatement pstmt = conn.prepareStatement(sqlStr);
//pstmt.setArray(1,Integer.parseInt(id));
//pstmt.setInt(1,Integer.parseInt(id));
// for (int i = 0; i < id.length; i++)
//if (id !=null){
// for (int i = 0; i < id.length; i++){
// pstmt.setString(1, id[i]);
pstmt.setString(1, id[i]);
int rec=pstmt.executeUpdate();
conn.close();
if (rec>0){
%>
<script>
alert("record is deleted!");
window.location.reload("allFood2.jsp");
</script>
<%
} //end if
else{
%>
<script>
alert(" no record is deleted!");
window.location.reload("allFood2.jsp");
</script>
<%
} //end else
}//end try
catch (Exception e) {
out.println(e.getMessage());
}
%>
答案 0 :(得分:0)
您可以创建一个字符串参数来保存ids
的CSV值,然后修改查询以用作参数。
String ids = "";
if (id != null && id.length != 0) {
out.println("You have selected: ");
for (int i = 0; i < id.length; i++) {
ids += id[i] +",";
out.println(id[i]);
} //end for loop
}// end if
if (ids .endsWith(","))
ids = ids.substring(0, ids .length() - 1);
try {
if (!"".equals(ids){
String sqlStr = "Delete from food WHERE id in (?)";
PreparedStatement pstmt = conn.prepareStatement(sqlStr);
pstmt.setString(1, ids);
int rec=pstmt.executeUpdate();
conn.commit();
}
} finally {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
}