gpt4 book ai didi

java - 使用jsp页面删除数据库中的数据

转载 作者:行者123 更新时间:2023-11-29 07:06:31 25 4
gpt4 key购买 nike

我正在编写一个在线预订系统。我的代码有问题,用户可以使用 jsp 页面上的取消按钮取消预订。但我的代码不起作用。它无法从数据库中删除数据。我怎样才能做到这一点? Image of my page

myreservations.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Book Ticket</title>
</head>

<body background="http://www.teamarking.com/barcode/bar_background.jpg">
<form method="post" action="reservations.jsp">


<center>

<table border="1" width="30%" height="30%">
<tr>
<th><font color='#D18603'>ActivityID</font></th>
<th><font color='#D18603'>Username</font></th>
<th><font color='#D18603'>Ticket</font></th>
<th><font color='#D18603'>Cancel</font></th>
</tr>




<%
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");
String username = (String) request.getSession().getAttribute("username");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from reservation where username='" + username + "'");
while (rs.next()) {

String activityid = rs.getString("id");
username = rs.getString("username");
String buy = rs.getString("buy");

out.println("<tr>");
out.println("<td>" + activityid + "</td>");
out.println("<td>" + username + "</td>");
out.println("<td>" + buy + "</td>");
out.println("<td><b><form action='cancel.jsp'><input type='submit' name='cancel' value='Cancel Reservation'></form></b>");

out.println("</tr>");

}

st.close();

%>

</center>
</table>


<br><a href='success.jsp'>Back</a>

<br><br><a href='logout.jsp'>Log out</a>


</form>
</body>
</html>

取消.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<%

String AcivityID = request.getParameter("ActivityID");
String Username = request.getParameter("Username");
String Ticket = request.getParameter("Ticket");

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");

String sorgu = "delete from reservation where id='" + request.getParameter(AcivityID) + "'AND username='" + request.getParameter(Username) + "'AND buy='" + request.getParameter(Ticket) + "'";

java.sql.Statement st = con.createStatement();

int rowNum = st.executeUpdate(sorgu);
response.sendRedirect("cancelled.jsp");
st.close();
%>

最佳答案

您需要向每个取消表单添加一个隐藏输入字段,以传递正在取消的 ActivityID 的信息。

目前您的表单如下所示:

<form action='cancel.jsp'>
<input type='submit' name='cancel' value='Cancel Reservation'>
</form>

添加隐藏字段后,它看起来像:

<form action='cancel.jsp'>
<input type='submit' name='cancel' value='Cancel Reservation'>
<input type="hidden" name="activityID" value="25">
</form>

除非您将此额外信息传递给cancel.jsp,否则它不知道按下了众多取消按钮中的哪一个。对于用户来说,可能很清楚要取消哪个 Activity ,但是 cancel.jsp 位于表单的接收端,并且不知道该按钮位于表的哪一列。

如果您想在 SQL 语句中包含用户名、activityID 和 Ticket,则需要使用隐藏字段传递所有三个值。

关于java - 使用jsp页面删除数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41401262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com