我有一个简单的Web应用程序,可以在其中预订房间。如果我去预订的房间,我会列出我已预订的房间。 现在,我想实现一个简单的按钮来删除特定的保留。 在我的数据库中,我有:
hotel_booking
具有以下参数:
cid,cname,电子邮件,到达日期,离开日期,人,房间,type_room,评论,状态,uid。
现在我创建了:
cancel.jsp
这是代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<%
int id;
int person;
int room;
id = Integer.parseInt(request.getParameter("id"));
// out.println(id);
String cname = request.getParameter("cname");
// out.println(cname);
String email = request.getParameter("email");
String arrival = request.getParameter("arrival_date");
String departure = request.getParameter("departure_date");
person = Integer.parseInt(request.getParameter("person"));
room = Integer.parseInt(request.getParameter("room"));
String type_room = request.getParameter("type_room");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/HotelReservation","root" ,"123456789");
String del = "delete from hotel_booking where id='" + Integer.parseInt(request.getParameter("id")) + "'AND cname='" + request.getParameter(cname) + "'AND emal='" + request.getParameter(email) + "'";
java.sql.Statement st = con.createStatement();
int rowNum = st.executeUpdate(del);
response.sendRedirect("home.jsp");
st.close();
%>
在booked.jsp中,我为取消按钮插入了此表单:
<form action='cancel.jsp'>
<input type='submit' name='cancel' value='Cancel Reservation'>
<input type="hidden" name="id" value=\""+id+"\">
</form>
</div>
但是我收到这种错误:
HTTP Status 500 – Internal Server Error
Type Exception Report
Message An exception occurred processing [/cancel.jsp] at line [10]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: An exception occurred processing [/cancel.jsp] at line [10]
7:
8:
9:
10: id = Integer.parseInt(request.getParameter("id"));
11: // out.println(id);
12: String cname = request.getParameter("cname");
13: // out.println(cname);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:593)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:482)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NumberFormatException: For input string: "\""+id+"\""
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:580)
java.lang.Integer.parseInt(Integer.java:615)
org.apache.jsp.cancel_jsp._jspService(cancel_jsp.java:128)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:444)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
我该如何解决?每当有人预订房间时,我的数据库中的cid参数就会自动增加一个。 谢谢大家
答案 0 :(得分:0)
尝试以下代码
<form action='cancel.jsp'>
<input type="hidden" name="id" value="<%=id%>">
<input type='submit' name='cancel' value='Cancel Reservation'>
</form>
</div>
因为id的值以“ \”“ + id +” \“”的形式传递。但是在您的代码中,您试图解析包含非数字值的字符串,因为Integer引发NumberFormatException。