我正在寫一個在線預訂系統。我的代碼有問題,用戶可以通過jsp頁面上的取消按鈕取消他們的預訂。但我的代碼不起作用。它不能從數據庫中刪除數據。我怎樣才能做到這一點? 用jsp從數據庫中刪除數據頁面
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>
cancel.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();
%>
jsp內部的java代碼只是可怕的想法。 – GurV
加上sql注入漏洞吧 – GurV
剛學jsp – tripley