使用JSP将数据插入mySQL

时间:2017-09-10 13:12:57

标签: java mysql jsp jdbc

我的错误

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。

3 个答案:

答案 0 :(得分:1)

java.lang.NumberFormatException: For input string: "idnum"表示字符串值“idnum”无法转换为数字。

你可能打算写

int idnum = Integer.parseInt(idnumber);

因为那是你的参数“idnum”的值。你可以尝试使用它吗?

答案 1 :(得分:0)

除@ f1sh答案外,为防止表单中的非数字,请使用number而不是int

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

答案 2 :(得分:0)

您正在尝试解析从数据库中检索的值,然后将其存储在“idnumber”中,因此您必须在.parseInt()中使用“idnumber”而不是列名。

isIos