Servlet无法编辑数据库中的记录

时间:2015-01-08 17:39:40

标签: database jsp servlets edit record

我有一个应该从我的数据库更新记录的servlet。 我的代码肯定有问题因为tomcat显示错误:java.lang.NumberFormatException:null @ package_ergasia.EditProtein.doPost(EditProtein.java:36)就是这一行:int i = Integer.parseInt(request.getParameter("i"));

这是我的Servlet:

package package_ergasia;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;    


public class EditProtein extends HttpServlet 
{
    Connection connection;  
    Statement statement;
    ResultSet  resultset;


   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException 
   {     
    response.setContentType("text/html");
    Connection connection= null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "ergasia";    
    String user = "root";
    String password = "password";
    String id = request.getParameter("id");
    String name = request.getParameter("pname");
    String desc = request.getParameter("desc");
    String seq = request.getParameter("seq");  
    int i = Integer.parseInt(request.getParameter("i"));


          try { 
            connection = DriverManager.getConnection(url + dbName, user, password); 

            PreparedStatement ps = connection.prepareStatement("UPDATE protein SET pdb_id=?,proteinName=?,description=?,proteinSequence=? WHERE i=?");
            ps.setString(1, id);
            ps.setString(2, name);
            ps.setString(3, desc);
            ps.setString(4, seq);
            ps.setInt(5, i);

            ps.executeUpdate();   
            RequestDispatcher view = request.getRequestDispatcher("/InsertProtein");
            view.forward(request, response);                

        } catch (SQLException e) {            
            e.printStackTrace();

        }


   }
}  

以下是具有用户可以更新数据的表单的jsp:

<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" import="java.sql.*"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../CSS/mystyle.css">        
    </head>
    <body>
          <h1>Welcome</h1>
          <form method="post" action="/EditProtein">
              <fieldset style="width: 100px"> 
              <legend>Update protein table</legend>  
          <table border="1">

<% 
int count = 0;    


                if (session.getAttribute("EditData") != null) {
                    ArrayList al1 = (ArrayList) session.getAttribute("EditData");
                    Iterator itr = al1.iterator();

                    while (itr.hasNext()) {
                        count++;


                        ArrayList pList = (ArrayList) itr.next();
%>                              

                    <tr>  
                        <td>pdb_id</td> 
                         <td><input type="text" disabled="disabled" font="Verdana" name="id" value="<%=pList.get(0)%>"</td>                       
                    </tr>  
                    <tr>  
                        <td>Protein Name</td>  
                        <td><input type="text" font="Verdana" name="pname" value="<%=pList.get(1)%>"</td>                          
                    </tr>  
                    <tr>  
                        <td>Description</td>  
                        <td><input type="text" font="Verdana" name="desc" value="<%=pList.get(2)%>"</td>                          
                    </tr>  
                    <tr>  
                        <td>Sequence</td>  
                        <td><input type="text" font="Verdana" name="seq" value="<%=pList.get(3)%>"</td>                          
                    </tr> 
                     <tr>  
                        <td>i</td>  
                        <td><input type="number" disabled="disabled" font="Verdana" name="i" value="<%=pList.get(4)%>"</td>                          
                    </tr> 
                    <tr>  
                        <td><input type="submit" value="Update"></td>
                        <td><input type="reset" value="Reset"></td>
                    </tr>   
                    <% }} %>

          </table>
            </fieldset>   
        </form> 
    </body>
</html>

我感谢你的帮助!

2 个答案:

答案 0 :(得分:1)

这里可能的错误行是

int i = Integer.parseInt(request.getParameter(“i”));

您的上一页未正确设置request.getParameter(“i”)。 在这个servlet参数值中,get为null,或者是其他数字(如i76 - i不是数字)。

检查你的上一页我已经设定。

答案 1 :(得分:0)

检查参数i是否已发送。如果未设置Becaus,request.getParameter(“i”)将返回null,当您尝试将其转换为int时,您将收到错误