2017-05-31 51 views
0

我有一些問題拉我的下拉選項值,這是一個數字,並將其拉入我的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:發生異常 處理JSP頁[/insert-user.jsp]在線路[8]

5:字符串電子郵件=用request.getParameter( 「電子郵件」) ; 6:字符串 pass = request.getParameter(「pass」); 7:字符串 tno = request.getParameter(「tno」); 8:int group = Integer.parseInt(request.getParameter(「group」)); 9:int role = Integer.parseInt(request.getParameter(「role」)); 10:字符串 notes = request.getParameter(「notes」); 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:空 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

回答

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

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

非常感謝你,這個工作! –

1

您的問題是,你正在試圖獲得參數group但你select組件具有Group。發錯,只需使用小寫字母命名。