title: unable to update table using JSP/DAO/Servlet
body: <p>Hello i'm trying to create a page to update a row in my database table i'm using DAO/Servlet with JSP page </p>
  
  <p>DAO code:</p>
  
  <pre><code>public static AnimalUpdateBean updateAnimal(AnimalUpdateBean bean) {
  
            //preparing some objects for connection 
          PreparedStatement up = null;   
          Statement stmt = null;    
  
            String id = bean.getAnimalId();
            String aname = bean.getAnimalName();
            String dob = bean.getAnimalDob();  
            String gender = bean.getAnimalGender();  
            String breedid = bean.getAnimalBreed();  
            String remark = bean.getAnimalRemark();
            try 
             { 
              //connect to DB 
                currentCon = dbConnection.getConnection();
  
                up = currentCon.prepareStatement("update animal set aname = '"+aname+"' , gender = '"+gender+"', specie_id = '"
                        +breedid+"' , remark = '"+remark+"' where animal_id = '"+id+"'");
  
                up.executeUpdate();
  
                if (up.executeUpdate()&gt;=1){
                    stmt=currentCon.createStatement();
                    rs = stmt.executeQuery("select aname , dob, gender, specie_id , remark from animal where animal_id = '"+id+"'");
                }
  
  
            System.out.println("done");
         }
  
  
         catch (Exception ex) 
         {
            System.out.println("Log In failed: An Exception has occurred! " + ex);
         } 
  
         //some exception handling
         finally 
         {
            if (rs != null)   {try {rs.close();} catch (Exception e) {} rs = null;}
  
            if (stmt != null) {try {stmt.close();} catch (Exception e) {}stmt = null;}
  
            if (currentCon != null) {try {currentCon.close();} catch (Exception e) {}currentCon = null;}
         }
  
      return bean;
  
         }    
     }
  </code></pre>
  
  <p>UpdateAnimal.jsp code:</p>
  
  <pre><code>&lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; 
  &lt;%@ page import="java.util.ArrayList" %&gt;
  &lt;%@page import="content.*"%&gt;
  &lt;%@page import="java.sql.*"%&gt;
  &lt;%@page import="java.util.*"%&gt;
  &lt;%@ page session="true"%&gt;
  &lt;%@page import="java.io.*"%&gt;
  &lt;%@page import="java.net.*"%&gt;
  &lt;%@page import="javax.servlet.*"%&gt;
  &lt;%@ page language="java" 
           contentType="text/html; charset=windows-1256"
           pageEncoding="windows-1256" %&gt;
  
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1256"&gt;
  &lt;title&gt;Update Animal&lt;/title&gt;
  &lt;link rel="stylesheet"
        href="./css/styles.css"
        type="text/css"/&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;table class="title"&gt;
    &lt;tr&gt;&lt;th&gt;Zoo keeper&lt;/th&gt;&lt;/tr&gt;
  &lt;/table&gt;
  
  
  &lt;h1&gt;Update Animal&lt;/h1&gt;
  
  &lt;form action="Relay" &gt;
  &lt;fieldset&gt;
  Animal new name: &lt;input type= "text" name = "aname"&gt;&lt;br&gt;
  Animal new DOB: &lt;input type= "text" name = "dob"&gt;&lt;br&gt;
  
  &lt;br&gt;
  Animal new gender: 
  &lt;select name="gender" id="gender"&gt;
  &lt;option value="male"&gt;Male&lt;/option&gt;
  &lt;option value="female"&gt;Female&lt;/option&gt;
  &lt;/select&gt;
  &lt;br&gt;
  
  Animal new Breed: &lt;input type= "text" name = "breedid" &gt;&lt;br&gt;
  Animal new remarks: &lt;textarea name = "remark" rows="4" cols="20"&gt;
  
  &lt;/textarea&gt; &lt;br /&gt; &lt;br/&gt;
  
  
  &lt;input type="submit" value="submit"&gt;
  &lt;input type="hidden" name="animal_id" value="&lt;%= request.getParameter("animal_id") %&gt;"&gt;
  &lt;input type="hidden" name="command" value="UpdateAnimalServlet" &gt; 
  
  &lt;/fieldset&gt;
  &lt;/form&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>the record being updated come from a select page to view the records when the user click on the name he will be redirected to update page CheckAnimal.jsp </p>
  
  <pre><code>&lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; 
  &lt;%@ page import="java.util.ArrayList" %&gt;
  &lt;%@page import="content.*"%&gt;
  &lt;%@page import="java.sql.*"%&gt;
  &lt;%@page import="java.util.*"%&gt;
  &lt;%@ page session="true"%&gt;
  &lt;%@page import="java.io.*"%&gt;
  &lt;%@page import="java.net.*"%&gt;
  &lt;%@page import="javax.servlet.*"%&gt;
  &lt;%@ page language="java" 
           contentType="text/html; charset=windows-1256"
           pageEncoding="windows-1256" %&gt;
  
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1256"&gt;
  &lt;title&gt;Animal list&lt;/title&gt;
  &lt;link rel="stylesheet"
        href="./css/styles.css"
        type="text/css"/&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;table class="title"&gt;
    &lt;tr&gt;&lt;th&gt;Zoo keeper&lt;/th&gt;&lt;/tr&gt;
  &lt;/table&gt;
  
  
  &lt;h1&gt;Animal list&lt;/h1&gt;
  Click on animal name to update it!
   &lt;center&gt;
   &lt;table width="100 % " id='table1'  border="1" cellspacing="2" cellpadding="2"&gt; 
      &lt;tr class="tab-highlighted-2"&gt; 
  
          &lt;td class="tab-highlighted-2" width="15"&gt; 
            &lt;div align="left"&gt;Name&lt;/div&gt; 
          &lt;/td&gt; 
          &lt;td class="tab-highlighted-2" width="13"&gt; 
            &lt;div align="left"&gt;Age&lt;/div&gt; 
          &lt;/td&gt;
           &lt;td class="tab-highlighted-2" width="13"&gt; 
            &lt;div align="left"&gt;Gender&lt;/div&gt; 
          &lt;/td&gt;
          &lt;td class="tab-highlighted-2" width="13"&gt; 
            &lt;div align="left"&gt;Status&lt;/div&gt; 
          &lt;/td&gt;    
          &lt;td class="tab-highlighted-2" width="13"&gt; 
            &lt;div align="left"&gt;Breed&lt;/div&gt; 
          &lt;/td&gt;  
          &lt;td class="tab-highlighted-2" width="13"&gt; 
            &lt;div align="left"&gt;Pen #&lt;/div&gt; 
          &lt;/td&gt; 
          &lt;td class="tab-highlighted-2" width="15"&gt; 
            &lt;div align="left"&gt;Zoo&lt;/div&gt; 
          &lt;/td&gt; 
          &lt;td class="tab-highlighted-2" width="20"&gt; 
            &lt;div align="left"&gt;Remarks&lt;/div&gt; 
          &lt;/td&gt; 
  
      &lt;/tr&gt; 
  
      &lt;c:forEach items="${beans}" var="view"&gt; 
          &lt;tr&gt; 
  
              &lt;td&gt;&lt;a href="/oosd/UpdateAnimal.jsp?animal_id=${view.animalId}&amp;breed=${view.breedId}"&gt;${view.animalName}&lt;/a&gt;&lt;/td&gt;  
              &lt;td&gt;${view.animalDob}&lt;/td&gt;
              &lt;td&gt;${view.animalGender}&lt;/td&gt;
              &lt;td&gt;${view.animalSource}&lt;/td&gt;
              &lt;td&gt;${view.animalBreed}&lt;/td&gt;
              &lt;td&gt;${view.sectionId}&lt;/td&gt;
              &lt;td&gt;${view.zooId}&lt;/td&gt;
              &lt;td&gt;${view.animalRemark}&lt;/td&gt;
  
          &lt;/tr&gt; 
      &lt;/c:forEach&gt; 
  &lt;/table&gt; 
  
           &lt;/center&gt;
  
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>AnimalUpdateBean.java code:</p>
  
  <pre><code>package content;
  
  public class AnimalUpdateBean {
      private String animalId;
      private String animalName;
      private String animalDob;
      private String animalGender;
      private String animalBreed;
      private String animalRemark;
  
      public String getAnimalId() {return animalId;}
      public String getAnimalName() {return animalName;}
      public String getAnimalDob() {return animalDob;}
      public String getAnimalGender() {return animalGender;}
      public String getAnimalBreed() {return animalBreed;}
      public String getAnimalRemark() {return animalRemark;}
  
      public void setAnimalId(String animalId) {this.animalId = animalId;}
      public void setAnimalName(String animalName) {this.animalName = animalName;}
      public void setAnimalDob(String animalDob) {this.animalDob = animalDob;}
      public void setAnimalGender(String animalGender) {this.animalGender = animalGender;}
      public void setAnimalBreed(String animalBreed) {this.animalBreed = animalBreed;}
      public void setAnimalRemark(String animalRemark) {this.animalRemark = animalRemark;}
  
  
  }
  </code></pre>
  
  <p>the servlet responsible to start the update is UpdateAnimalServlet:</p>
  
  <pre><code>package content;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  /**
   * Servlet implementation class LoginServlet
   */
  public class UpdateAnimalServlet extends HttpServlet {
  
  
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
                         throws ServletException, java.io.IOException {
  
  try
  {       
  
       AnimalUpdateBean animal = new AnimalUpdateBean();
       animal.setAnimalId(request.getParameter("animal_id"));
       animal.setAnimalName(request.getParameter("aname"));
   //    animal.setAnimalDob(request.getParameter("dob"));
       animal.setAnimalGender(request.getParameter("gender"));
       animal.setAnimalBreed(request.getParameter("breedid"));
       animal.setAnimalRemark(request.getParameter("remark"));
       String test = request.getParameter("animal_id");
       System.out.println(test);
       System.out.println(animal);
  
       animal = DAO.updateAnimal(animal);
  
  
            response.sendRedirect("/oosd/member.jsp"); 
  
  
  } 
  
  
  catch (Throwable theException)      
  {
       System.out.println(theException); 
  }
         }
      }
  </code></pre>
  
  <p>I'm using a Relay servlet that calls AnimalUpdateServlet for execution:</p>
  
  <pre><code>package content;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  /**
   * Servlet implementation class LoginServlet
   */
  public class Relay extends HttpServlet {
  
  
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
                         throws ServletException, java.io.IOException {
  
      try
      {       
  
  
  
          String command = request.getParameter("command");  
          if (command.equals("LoginServlet")){ 
              RequestDispatcher rd =request.getRequestDispatcher("/"+command);
              rd.forward(request, response);
              //for testing 
              System.out.println("Request forwarded to " + command + " servlet"); 
          } else if (command.equals("UpdateAnimalServlet")){ 
              RequestDispatcher rd =request.getRequestDispatcher("/"+command);
              rd.forward(request, response);
              //for testing 
              System.out.println("Request forwarded to " + command + " servlet"); 
          } 
  
          else 
              System.out.println("=&gt; command='" + command + "'");  
              String url = "/oosd/" + command;     
              String encodedUrl = response.encodeRedirectURL(url);   
              System.out.println("   url=" + url);    
              System.out.println("   encodedUrl=" + encodedUrl);  
              response.sendRedirect(encodedUrl);
  
  
  
      } 
  
  
      catch (Throwable theException)      
      {
           System.out.println(theException); 
      }
             }
          }
  
  
  and last thing is the web.xml page:
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
    &lt;servlet&gt;
      &lt;servlet-name&gt;LoginServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;content.LoginServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;UpdateAnimalServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;content.UpdateAnimalServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;SelectAnimalServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;content.SelectAnimalServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;Relay&lt;/servlet-name&gt;
      &lt;servlet-class&gt;content.Relay&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;SelectAnimalServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/SelectAnimalServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;UpdateAnimalServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/UpdateAnimalServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;LoginServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/LoginServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Relay&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/Relay&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>I keep getting these errors:
  Log In failed: An Exception has occurred! java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.</p>
  
  <p>java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed</p>
  
  <p>I have checked the data entry and all correct name text, gender text, specie_id number, remark text i even put the dob on hold until this work i'm using access database and here is the connection code:</p>
  
  <pre><code>package content;
  
  
  import java.sql.*;
  
  
  public class dbConnection {
  
     static Connection con;
     static String url;
  
     public static Connection getConnection()
     {
  
        try
        {
           String url = "jdbc:odbc:oosd"; 
           // assuming "DataSource" is your DataSource name
  
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  
           try
           {              
              con = DriverManager.getConnection(url,"",""); 
  
           // assuming your SQL Server's  username is "username"               
           // and password is "password"
  
           }
  
           catch (SQLException ex)
           {
              ex.printStackTrace();
           }
        }
  
        catch(ClassNotFoundException e)
        {
           System.out.println(e);
        }
  
     return con;
  }
  }
  </code></pre>
  
  <p>Any idea where is my mistake here ?</p>