将下拉值拉入整数

时间:2017-05-31 19:49:01

标签: java mysql jsp

我有一些问题拉下我的下拉选项值,这是一个数字,并将其拉入我的MySQL查询。这里有一些代码供您使用:

users.jsp

<form method="post" action="insert-user.jsp"> 
<strong>Add User</strong>
<table>
<tr><td>First Name:</td><td><input type="text" name="fname"></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lname"></td></tr>
<tr><td>Account email:</td><td><input type="text" name="email"></td></tr>
<tr><td>Account password:</td><td><input type="password" name="pass"></td></tr>
<tr><td>Phone number:</td><td><input type="text" name="tno"></td></tr>
<tr><td>Group:</td><td><label for = "group"></label>
        <select name = "Group" id="group">
            <option value="1" selected>CSOTF-I</option>
            <option value="2">CSOTF-S</option>
            <option value="3">PACCOM</option></select></td></tr>
<tr><td>Role:</td><td><label for = "role"></label>
        <select name = "role" id="role">
            <option value="1" selected>Admin</option>
            <option value="2">FSR</option>
            <option value="3">Advisor</option></select></td></tr>
<tr><td>Notes:</td><td><input type="text" name="notes"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>

并插入-user.jsp

<%@page import="java.sql.*,java.util.*"%> 
<%
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String email=request.getParameter("email");
String pass=request.getParameter("pass");
String tno=request.getParameter("tno");
int group=Integer.parseInt(request.getParameter("group"));
int role=Integer.parseInt(request.getParameter("role"));
String notes=request.getParameter("notes");


        try{
         Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/raa", "root", "root");
           Statement st=con.createStatement();
           int i=st.executeUpdate("insert into tbluser(fname,lname,phone,email,password,notes,groupID,roleID) values('"+fname+"','"+lname+"','"+tno+"','"+email+"','"+pass+"','"+notes+"','"+group+"','"+role+"')");
        out.println("Data is successfully inserted!");
        response.sendRedirect("users.jsp");
        }
        catch(Exception e){
        System.out.print(e);
        e.printStackTrace();
        }
        %>

我得到的500错误:

  

HTTP状态[500] - [内部服务器错误]

     

类型例外报告

     

消息处理JSP页面时出现异常[/insert-user.jsp]   在第[8]行

     

描述服务器遇到意外情况   阻止它履行请求。

     

异常org.apache.jasper.JasperException:发生异常   在第[8]行处理JSP页面[/insert-user.jsp]

     

5:String email = request.getParameter(&#34; email&#34;); 6:字符串   通=用request.getParameter(&#34;通过&#34); 7:字符串   TNO =用request.getParameter(&#34; TNO&#34); 8:int   基团=的Integer.parseInt(用request.getParameter(&#34;组&#34)); 9:int   角色=的Integer.parseInt(用request.getParameter(&#34;作用&#34)); 10:字符串   笔记=用request.getParameter(&#34;笔记&#34); 11:

     

堆栈跟踪:     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

     

根本原因java.lang.NumberFormatException:null     java.lang.Integer.parseInt(未知来源)     java.lang.Integer.parseInt(未知来源)     org.apache.jsp.insert_002duser_jsp._jspService(insert_002duser_jsp.java:122)     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

     

注意服务器中提供了根本原因的完整堆栈跟踪   日志。

     

Apache Tomcat / 9.0.0.M21

2 个答案:

答案 0 :(得分:1)

您的问题是,您尝试获取参数 group,但您的select组件的名称 Group。输入拼写错误,只需使用小写进行命名。

答案 1 :(得分:1)

Integer group = (request.getParameter("group") != null) ? Integer.parseInt(request.getParameter("group")) : null;

<select name="group" id="group">