title: Unable to update a particular record in table using servlet and jsp
body: <p>Hey guys I have created a jsp page for user where they can update their existing details and the record get updated in database on submit.</p>
  
  <p>1.> <strong>Accountdetails.java</strong></p>
  
  <pre><code>//Java Servlet program responsible for obtaining the user data and updating into   database    
  package roseindia.net;
  
  import java.io.*;
  import java.sql.*;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet; 
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
   * Servlet implementation class Accountdetails 
   */
   @WebServlet("/Accountdetails")
   public class Accountdetails extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
   /**
    * @see HttpServlet#HttpServlet()
   */
  public Accountdetails() {
      super();
      // TODO Auto-generated constructor stub
  }
  
  
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
      // TODO Auto-generated method stub
      System.out.println("Servlet is in the doGet method of account details");
  
  
  
  
  }
  
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      System.out.println("Servlet is in the doPost method of account details");
  
      response.setContentType("text/html");  
      PrintWriter pw = response.getWriter(); 
      pw.println("&lt;html&gt;");
      pw.println("&lt;body&gt;");
      pw.println("&lt;h1&gt;Hello Servlet Post&lt;/h1&gt;");
      pw.println("&lt;/body&gt;");
      pw.println("&lt;/html&gt;");
  
  
      Connection conn=null;
      String url="jdbc:mysql://localhost:3306/";
      String dbName="userlogindb";
      String driver="com.mysql.jdbc.Driver";
  
      try{  
        String Fname = request.getParameter("fname");  
        String Mname = request.getParameter("mname");  
        String Lname = request.getParameter("lname");  
        String Uname = request.getParameter("username");  
        String Emailid = request.getParameter("emailid");  
        String Mobno = request.getParameter("mobno");  
        String Address = request.getParameter("address");  
        String Password1 = request.getParameter("password1");  
        String Password2 = request.getParameter("password2");
        String ID = request.getParameter("id");
  
        Class.forName(driver);  
        conn = DriverManager.getConnection(url+dbName,"root", "root");
  
            String update="update registerutable set fname='"+Fname+"', mname='"+Mname+"',    lname='"+Lname+"', username='"+Uname+"', emailid='"+Emailid+"', mobno='"+Mobno+"',    address='"+Address+"', password1='"+Password1+"', password2='"+Password2+"' where    id='"+ID+"' ";   
  
                  System.out.println("The sql is " +update);
                       PreparedStatement pst = (PreparedStatement) conn.prepareStatement(update);
  
  
        pst.setString(1,Fname);  
        pst.setString(2,Mname);        
        pst.setString(3,Lname);
        pst.setString(4,Uname);
        pst.setString(5,Emailid);
        pst.setString(6,Mobno);
        pst.setString(7,Address);
        pst.setString(8,Password1);
        pst.setString(9,Password2);
        pst.setString(11,ID);
  
  
        int i = pst.executeUpdate();
  
        String msg=" ";
        if(i!=0){  
          msg="User Updated";
  
          pw.print(i);
          pw.print("&lt;font size='6' color=blue&gt;" + msg + "&lt;/font&gt;");  
          pw.print(" '"+Fname+"'");
  
        }  
        else{  
          msg="failed to update the data";
          pw.print("&lt;font size='6' color=blue&gt;" + msg + "&lt;/font&gt;");
         }  
        pw.close();
        pst.close();
        conn.close();
      }  
      catch (Exception e){  
          e.printStackTrace(); 
      }  
  
  }
  
   }
  </code></pre>
  
  <p>2.> <strong>successful.jsp</strong></p>
  
  <pre><code>//JSP page which shows existing value and new value to be entered by user.
  
  &lt;form name="myaccountform" action="Accountdetails" method="post"&gt;
  &lt;table border="1"&gt;
  &lt;tr&gt;
  &lt;td&gt;First name:&lt;/td&gt;
  &lt;%Object value11 = request.getAttribute("fname");%&gt;
  &lt;td&gt;&lt;%=value11%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Enter the new first name:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="fname" value=""&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Middle name:&lt;/td&gt;
  &lt;%Object value12 = request.getAttribute("mname");%&gt;
  &lt;td&gt;&lt;%=value12%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Enter the new middle name:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="mname" value=""&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Last name:&lt;/td&gt;
  &lt;%Object value13 = request.getAttribute("lname");%&gt;
  &lt;td&gt;&lt;%=value13%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Enter the new last name:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="lname" value=""&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Username:&lt;/td&gt;
  &lt;%Object value14 = request.getAttribute("uname");%&gt;
  &lt;td&gt;&lt;%=value14%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Enter new username:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="username" value=""&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Email id:&lt;/td&gt;
  &lt;%Object value15 = request.getAttribute("emailid");%&gt;
  &lt;td&gt;&lt;%=value15%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Enter new email id:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="emailid" value=""&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Mobile number:&lt;/td&gt;
  &lt;%Object value16 = request.getAttribute("mobno");%&gt;
  &lt;td&gt;&lt;%=value16%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Enter new mobile number:&lt;/td&gt;
  &lt;td&gt;&lt;input type="text" name="mobno" value=""&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Address:&lt;/td&gt;
  &lt;%Object value17 = request.getAttribute("address");%&gt;
  &lt;td&gt;&lt;%=value17%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Enter new address:&lt;/td&gt;
  &lt;td&gt;&lt;textarea rows="5" cols="15" name="address"&gt;&lt;/textarea&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Old password:&lt;/td&gt;
  &lt;%Object value18 = request.getAttribute("password1");%&gt;
  &lt;td&gt;&lt;%=value18%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Enter the new password:&lt;/td&gt;
  &lt;td&gt;&lt;input type="password" name="password1" value=""&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;Reenter the new password:&lt;/td&gt;
  &lt;td&gt;&lt;input type="password" name="password2" value=""&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;input type="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>3.> <strong>web.xml</strong></p>
  
  <pre><code>//mapping between servlet and jsp
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
  &lt;display-name&gt;LoginExample&lt;/display-name&gt;
  &lt;welcome-file-list&gt;
  &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
  &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
  &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
  &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
  &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
  &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  
  &lt;servlet&gt;
  &lt;description&gt;Login Servlet&lt;/description&gt;
  &lt;display-name&gt;Login&lt;/display-name&gt;
  &lt;servlet-name&gt;Login&lt;/servlet-name&gt;
  &lt;servlet-class&gt;roseindia.net.Login&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;Login&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/login&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;servlet&gt;
  &lt;description&gt;Servlet Example&lt;/description&gt;
  &lt;display-name&gt;ServletExample&lt;/display-name&gt;
  &lt;servlet-name&gt;ServletExample&lt;/servlet-name&gt;
  &lt;servlet-class&gt;servletexample.com.ServletExample&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;ServletExample&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/register&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;servlet&gt;
  &lt;description&gt;Account Details&lt;/description&gt;
  &lt;display-name&gt;Accountdetails&lt;/display-name&gt;
  &lt;servlet-name&gt;Accountdetails&lt;/servlet-name&gt;
  &lt;servlet-class&gt;roseindia.net.Accountdetails&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;Accountdetails&lt;/servlet-name&gt;
  &lt;url-pattern&gt;/Accountdetails&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>The problem here is when fill up all the fields and click on submit it is unable to update it.</p>
  
  <p>It goes into else part showing failed to update the data.</p>
  
  <p>When I sys out it shows The sql is  </p>
  
  <pre><code>update registerutable set 
    fname='admin11', mname='admin11', lname='admin11', username='admin11',
    emailid='admin', mobno='1111', address='admin', password1='admin',  
    password2='admin' 
  where id='null'
  </code></pre>
  
  <p>I also tried  </p>
  
  <pre><code>String ID = request.getParameter("id");
  </code></pre>
  
  <p>to  </p>
  
  <pre><code>int ID = Integer.parseInt(request.getParameter("id")); 
  </code></pre>
  
  <p>but still unable to update it.</p>
  
  <p>Please help me to correct as I dont know where m I going wrong now.</p>