这是我的表格。我将参数发送到servlet,在该servlet中我进行了更新查询,但是查询没有执行,因为它显示为NumberFormatException。
`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="updateS" method="post">
<table>
<tr>
<th colspan="2" >Update table</th>
</tr>
<tr>
<td>Roll no:</td>
<td><input type="text" name="roll" value=""/></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name" value=""/></td>
</tr>
<tr>
<td>Class:</td>
<td><input type="text" name="class" value=""/></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" value=""/></td>
</tr>
<tr>
<td><input type="submit" value="Update"/></td>
<td><input type="reset" value="Reset"/></td>
</tr>
</table>
</form>
</body>
</html>这是servlet,在第83行发生了numberformatException。
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
processRequest(request, response);
PrintWriter out = response.getWriter();
String rollno=request.getParameter("roll");
**int roll= Integer.valueOf(rollno);**
String classname=request.getParameter("class");
String name = request.getParameter("name");
String address = request.getParameter("address");
System.out.println(address);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kamal", "root", "123");
PreparedStatement ps = con.prepareStatement("update result set name=?, class=?, address=? where rollno=?");
ps.setString(1, name);
ps.setString(2, classname);
ps.setString(3, address);
ps.setInt(4, roll);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
finally{
}
}我按如下所示更改了上面的代码,但问题仍然存在。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
processRequest(request, response);
PrintWriter out = response.getWriter();
String rollno=request.getParameter("roll");
int roll = 0;
if(rollno!=null && !rollno.trim().equals("")){
roll = Integer.parseInt(rollno);
}
String classname=request.getParameter("class");
String name = request.getParameter("name");
String address = request.getParameter("address");
System.out.println(address);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kamal", "root", "123");
PreparedStatement ps = con.prepareStatement("update result set name=?, class=?, address=? where rollno=?");
ps.setString(1, name);
ps.setString(2, classname);
ps.setString(3, address);
ps.setInt(4, roll);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}`
发布于 2016-10-26 13:08:10
根据您的异常,您可以将null值解析为Integer.valueOf()方法。为了避免异常,请使用这个。
int roll = 0;
if(rollno!=null && !rollno.trim().equals("")){
roll = Integer.parseInt(rollno);
}然后使用下面的方法:
public Integer isInteger(String str) {
int roll=0;
try {
roll= Integer.parseInt(str);
} catch (NumberFormatException e) {
return 0; // The string isn't a valid number
}
return roll;
}
int roll = this.isInteger(rollno); Use this on your code发布于 2016-10-26 14:08:24
这是另一个项目,它使用相同的代码。
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial- scale=1.0">
</head>
<body>
<form action="Servlet" method="POST">
Enter your Rollno:<input type="text" name="roll"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>这就是servlet
public class NewServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String rollno = request.getParameter("roll");
int roll = Integer.valueOf(rollno);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kamal", "root", "123");
PreparedStatement ps = con.prepareStatement("select * from result where rollno=?");
ps.setInt(1, roll);
out.print("<table width=50% border=1>");
out.print("<caption>Result:</caption>");
ResultSet rs = ps.executeQuery();
/* Printing column names */
ResultSetMetaData rsmd = rs.getMetaData();
int total = rsmd.getColumnCount();
out.print("<tr>");
for (int i = 1; i <= total; i++) {
out.print("<th>" + rsmd.getColumnName(i) + "</th>");
}
out.print("</tr>");
/* Printing result */
while (rs.next()) {
out.print("<tr><td>" + rs.getInt(1) + "</td><td>" + rs.getString(2) +"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td> </tr>");
}
out.print("</table>");
} catch (Exception e2) {
e2.printStackTrace();
} finally {
out.close();
}
}
}https://stackoverflow.com/questions/40253899
复制相似问题