[英]Why is the delete function not working in my jsp file?
我已經創建了一個表來顯示數據,並且每個數據旁邊都有一個復選框。 我想創建一個函數,如果用戶單擊該數據的復選框並按Delete鍵,則該數據將從表和數據庫中刪除。 我想出了代碼,但它不會刪除數據。 我的代碼有什么問題?
的HTML
<%
String id = request.getParameter("hiddenID");
String sql1="";
{
sql1 = "select * from exercise1";
PreparedStatement pstmt1=conn.prepareStatement(sql1);
ResultSet rs1 = pstmt1.executeQuery();
while(rs1.next()){
String arm = rs1.getString("Arm");
String time1 = rs1.getString("Time1");
out.println("<tr>");
out.println("<td style = 'width: 20%'>");
out.println(arm);
out.println("</td>");
out.println("<td style = 'width: 5%'>");
out.println(time1);
out.println("</td>");
%>
<td style="width: 5%"><input class="mychkbox" type="checkbox"
value="<%=id%>" form="multipleDele" name="DeleteChkbox" /></td>
<%
out.println("</tr>");
}
conn.close();
}
%>
這是我的delete.jsp文件
<%
String[] id = request.getParameterValues("DeleteChkbox");
int count=0;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// Step 2: Define Connection URL
String connURL = "jdbc:mysql://localhost/medicloud?user=root&password=root";
// Step 3: Establish connection to URL
conn = DriverManager.getConnection(connURL);
if (id != null)
{
for(int i=0;i<id.length;i++){
String sqlStr = "DELETE from exercise1 WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sqlStr);
pstmt.setInt(1, Integer.parseInt(id[i]));
//pstmt.setInt(1, Integer.parseInt(id[i]));
int rec=pstmt.executeUpdate();
if (rec==1)
count++;
}
}
//System.out.println(functions);
%>
<form action="exercise.jsp" method="post">
<label><%=count%> record(s) deleted!!</label>
<input type="submit" value="Return" name="ReturnBtn" />
</form>
<%
conn.close();
} catch (Exception e) {
out.println(e);
}
%>
您正在寫入復選框值的id
並非來自數據庫結果。 它是在JSP中從請求參數中檢索的:
String id = request.getParameter("hiddenID");
...然后寫入每個復選框字段:
<input class="mychkbox" type="checkbox" value="<%=id%>" form="multipleDele" name="DeleteChkbox" />
您不會為每個復選框寫一個不同的ID。
因此,它可能不是任何數據庫記錄的ID,因此都不會刪除它們。
構建HTML時,您應該在while循環中讀取每個記錄的id
。
我懷疑您需要這樣的東西:
<input class="mychkbox" type="checkbox" value="<%=rs1.getString("id")%>" form="multipleDele" name="DeleteChkbox" />
您的圖像分為三列:手臂鍛煉,次數和動作。 DeleteChkboox
字段僅會告訴您是否為特定行選擇了該字段。 您需要獲取已選擇刪除的ID。 未選中的復選框不會顯示在一系列復選框中,因此您的表單必須考慮到這一點。 它需要看起來像
<form action = "...">
<input type="submit" value="delete">
<input type="submit" value="assign">
<table>
<c:forEach items="${ records }" var="r">
<tr>
<td>
${ r.exercise }
</td>
<td>
${ r.times }
</td>
<td>
<input type="checkbox" value="${ r.id }" name="userAction">
</td>
</tr>
</c:forEach>
</table>
</form>
然后,您需要更改jsp,以便獲取所選行的ID。 更改將您的ID迭代到的JSP部分
<%
// get array of ids
String[] ids = request.getParameterValues("userAction");
int count=0;
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// Step 2: Define Connection URL
String connURL = "jdbc:mysql://localhost/medicloud?user=root&password=root";
// Step 3: Establish connection to URL
conn = DriverManager.getConnection(connURL);
if (ids != null) {
for(int i=0; i < ids.length; i++) {
String sqlStr = "DELETE from exercise1 WHERE id=?";
PreparedStatement pstmt = conn.prepareStatement(sqlStr);
pstmt.setInt(1, Integer.parseInt(ids[i]));
int rec=pstmt.executeUpdate();
if (rec==1) {
count++;
}
}
}
}
%>
除此之外,如果可以幫助的話,在jsps中運行sql更新通常不是最好的主意。 考慮將這種邏輯移至控制器類,並使用jsp層進行演示。
編輯
根據您的評論“它不起作用”,您不必看這個特定的問題(為什么您編寫的代碼不能刪除數據),而您需要看的是更大的圖景:您要解決的問題是什么?
用簡單的英語看起來像這樣:
第1步
遍歷jsp中<form>
元素內的記錄列表對於每個記錄,創建一個復選框,其值為該記錄的ID
第2步
添加刪除按鈕,允許用戶提交ID形式
第三步
處理提交的信息:獲取在請求中提交的ID列表,對其進行迭代並逐一刪除記錄
你堅持哪一步?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.