从数据库中删除一行

时间:2018-07-11 09:03:55

标签: java sql database jsp

我有一个简单的Web应用程序,您可以在其中预订房间。现在,当有人预定房间时,我希望可以删除预订。 我创建了一个jsp文件:

  

cancel.jsp

使用以下代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*,java.util.*"%>
<%
String id=request.getParameter("cid");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HotelReservation", "root", "123456789");
Statement st=conn.createStatement();
int i=st.executeUpdate("DELETE FROM hotel_booking WHERE cid="+id);
out.println("Data Deleted Successfully!");
}
catch(Exception e)
{
System.out.print(e);
e.printStackTrace();
}
%>

和booked.jsp中:

<%  while(rs.next())
        {

           int id = rs.getInt("cid");

          //  out.println(id);
            String cname  = rs.getString("cname");
           // out.println(cname);
            String email  = rs.getString("email");
            String arrival  = rs.getString("arrival_date");
            String departure  = rs.getString("departure_date");
            int person  = rs.getInt("person");
            int room  = rs.getInt("room");
            String type_room = rs.getString("type_room");



            %> 

            <div class="hotel-block"  style="background-color: #5882FA;padding: 2px 15px 30px;margin-top: -20px; width: 102%; margin-left: -23px;">
                ...........................................................................................................................................................................................................................................................................................
                <h2 class="heading"><label style=" margin-left: 150px; margin-top: 151px;">Name :</label><span  style=" margin-left: 200px;"><%out.println(cname);%></h2> 
                <h2 class="heading"><label style="margin-left: 150px;">Email :</label><span  style=" margin-left: 200px;"><%out.println(email);%></span></h3> 
                <h2 class="heading"><label style="margin-left: 150px;">arrival date :</label><span  style=" margin-left: 100px;"><%out.println(arrival);%></h2>
                <h2 class="heading"><label style="margin-left: 150px;">departure date:</label><span  style=" margin-left: 50px;"><%out.println(departure);%></h2> 
                <h2 class="heading"><label style="margin-left: 150px;">no. of person :</label><span  style=" margin-left: 70px;"><%out.println(person);%></h2>
                <h2 class="heading"><label style="margin-left: 150px;">no. of room :</label><span  style=" margin-left: 100px;"><%out.println(room);%></h2> 
                <h2 class="heading"><label style="margin-left: 150px;">type room:</label><span  style=" margin-left: 100px;"><%out.println(type_room);%></h2>

               <form action='cancel.jsp'>
               <input type="hidden" name="cid" value="<%=("id") %>">
             <input type='submit' name='cancel' value='Cancel Reservation'>

            </form>
           </div>

我的数据库中也有此表:hotel_booking,其中包含以下参数:

  

cid(INT,主键,不为null,自动递增);名字电子邮件;到达日期;离职日期; ....

我不知道如何删除特定的预订。每次有人预定房间时,cid都会自动增加一个。 您能帮我解决这个问题吗?谢谢

1 个答案:

答案 0 :(得分:2)

更改

<input type="hidden" name="cid" value="<%=("id") %>">

<input type="hidden" name="cid" value="<%= id%>">