使用jsp页面从数据库中删除数据

时间:2016-12-30 18:54:55

标签: java html mysql jsp

我正在写一个在线预订系统。我的代码有问题,用户可以使用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>

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();
%>

1 个答案:

答案 0 :(得分:2)

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

目前您的表单如下:

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

使用添加的隐藏字段,它看起来像:

 {
  "Username": "ozziep",
  "ProjectID": "ExpressJS",
  "TimeStamp": "2016-12-30T19:54:52.418Z",
  "Comments": "hello world how are we today?"
}

{
  "Username": "alex",
  "ProjectID": "Foo",
  "TimeStamp": "2016-12-30T19:55:07.138Z",
  "Comments": "we need to double check that this json system works. "
}

除非你将这些额外的信息传递给cancel.jsp,否则不知道按下了多少个取消按钮。对于用户来说,可能很清楚要取消哪些活动,但是cancel.jsp位于表单的接收端,并且不知道该按钮所在表的哪一列。

如果要在SQL语句中使用username,activityID和Ticket,则需要使用隐藏字段传递所有三个值。