Draw the drop-down value into an integer

advertisements

I'm having some issues pulling my dropdown option value, which is a number, and pull that into my MySQL Query. Here's some code for you:

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>

And insert-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();
        }
        %>

And the 500 error I'm getting:

HTTP Status [500] – [Internal Server Error]

Type Exception Report

Message An exception occurred processing JSP page [/insert-user.jsp] at line [8]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception org.apache.jasper.JasperException: An exception occurred processing JSP page [/insert-user.jsp] at line [8]

5: String email=request.getParameter("email"); 6: String pass=request.getParameter("pass"); 7: String tno=request.getParameter("tno"); 8: int group=Integer.parseInt(request.getParameter("group")); 9: int role=Integer.parseInt(request.getParameter("role")); 10: String notes=request.getParameter("notes"); 11:

Stacktrace: 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)

Root Cause java.lang.NumberFormatException: null java.lang.Integer.parseInt(Unknown Source) java.lang.Integer.parseInt(Unknown Source) 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)

Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/9.0.0.M21


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

and

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