2017-09-10 79 views
0

我的錯誤插入數據到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。

+0

您認爲什麼'int idnum = Integer.parseInt(「idnum」)'呢? –

回答

1

java.lang.NumberFormatException: For input string: "idnum"表示字符串值「idnum」不能轉換爲數字。

你大概的意思是寫

int idnum = Integer.parseInt(idnumber); 

,因爲這是你的參數「IDNUM」的價值。你可以嘗試使用它嗎?

0

除了@ f1sh答案,爲了防止在形式上利用類型number非數字,而不是int

<input text="idnum" type="number" placeholder="ID number"></td> 
0

您正在嘗試解析您是從數據庫中檢索值,然後將其保存在「的IDNumber」 ,所以你必須在.parseInt()中使用'idnumber'而不是列名。

String idnumber = request.getParameter("idnum"); 
     int idnum = Integer.parseInt("idnum");//old version 


String idnumber = request.getParameter("idnum"); 
     int idnum = Integer.parseInt(idnumber);//new version