我的錯誤插入數據到MySQL使用JSP
HTTP Status 500 - An exception occurred processing JSP page /insertdata.jsp at line 24
異常報告:
An exception occurred processing JSP page /insertdata.jsp at line 24
description The server encountered an internal error that prevented it from
fulfilling this request.
例外
org.apache.jasper.JasperException: An exception occurred processing JSP page
/insertdata.jsp at line 24
21: String fname = request.getParameter("firstname");
22: String emailadd = request.getParameter("email");
23: String idnumber = request.getParameter("idnum");
24: int idnum = Integer.parseInt("idnum");
25: try{
26: Class.forName("com.mysql.jdbc.Driver");
27: Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "root","7597");
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
java.lang.NumberFormatException: For input string: "idnum"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
java.lang.Integer.parseInt(Integer.java:580)
java.lang.Integer.parseInt(Integer.java:615)
org.apache.jsp.insertdata_jsp._jspService(insertdata_jsp.java:85)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
我插入代碼
<%
String lname = request.getParameter("lastname");
String fname = request.getParameter("firstname");
String emailadd = request.getParameter("email");
String idnumber = request.getParameter("idnum");
int idnum = Integer.parseInt("idnum");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/database",
"root","7597");
Statement st = con.createStatement();
st.executeUpdate("insert into dbtable(lastname, firstname, email,
idnumber)values('"+lname+"','"+fname+"','"+emailadd+"','"+idnum+"')");
out.println("data is inserted in the database");
}catch (Exception e){
out.println(e);
}
%>
我輸入的數據
<form action="insertdata.jsp">
<tr>
<td><input text="lastname" type="text" placeholder="Last name"></td>
<td><input text="firstname" type="text" placeholder="Fist name"></td>
<td><input text="email" type="email" placeholder="Email"></td>
<td><input text="idnum" type="int" placeholder="ID number"></td>
<td><input type="submit" value="Submit"/>
<input type="reset" value="Reset"/></td>
</tr>
</form>
我的數據庫
SELECT * FROM `database`.dbtable;SELECT `dbtable`.`id`,
`dbtable`.`lastname`,
`dbtable`.`firstname`,
`dbtable`.`email`,
`dbtable`.`idnum`
FROM `database`.`dbtable`;
我真的很困惑,爲什麼它不會插入到數據庫中。 我使用的是mysql workbench,netbeans編譯器和jsp。
您認爲什麼'int idnum = Integer.parseInt(「idnum」)'呢? –