Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6786471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:14:57+00:00 2026-05-26T17:14:57+00:00

Hello i’m trying to create a page to update a row in my database

  • 0

Hello i’m trying to create a page to update a row in my database table i’m using DAO/Servlet with JSP page

DAO 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()>=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;

       }    
   }

UpdateAnimal.jsp code:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page import="java.util.ArrayList" %>
<%@page import="content.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page session="true"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>
<%@ page language="java" 
         contentType="text/html; charset=windows-1256"
         pageEncoding="windows-1256" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Update Animal</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>
</head>
<body>
<table class="title">
  <tr><th>Zoo keeper</th></tr>
</table>


<h1>Update Animal</h1>

<form action="Relay" >
<fieldset>
Animal new name: <input type= "text" name = "aname"><br>
Animal new DOB: <input type= "text" name = "dob"><br>

<br>
Animal new gender: 
<select name="gender" id="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<br>

Animal new Breed: <input type= "text" name = "breedid" ><br>
Animal new remarks: <textarea name = "remark" rows="4" cols="20">

</textarea> <br /> <br/>


<input type="submit" value="submit">
<input type="hidden" name="animal_id" value="<%= request.getParameter("animal_id") %>">
<input type="hidden" name="command" value="UpdateAnimalServlet" > 

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

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

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page import="java.util.ArrayList" %>
<%@page import="content.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page session="true"%>
<%@page import="java.io.*"%>
<%@page import="java.net.*"%>
<%@page import="javax.servlet.*"%>
<%@ page language="java" 
         contentType="text/html; charset=windows-1256"
         pageEncoding="windows-1256" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Animal list</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>
</head>
<body>
<table class="title">
  <tr><th>Zoo keeper</th></tr>
</table>


<h1>Animal list</h1>
Click on animal name to update it!
 <center>
 <table width="100 % " id='table1'  border="1" cellspacing="2" cellpadding="2"> 
    <tr class="tab-highlighted-2"> 

        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Name</div> 
        </td> 
        <td class="tab-highlighted-2" width="13"> 
          <div align="left">Age</div> 
        </td>
         <td class="tab-highlighted-2" width="13"> 
          <div align="left">Gender</div> 
        </td>
        <td class="tab-highlighted-2" width="13"> 
          <div align="left">Status</div> 
        </td>    
        <td class="tab-highlighted-2" width="13"> 
          <div align="left">Breed</div> 
        </td>  
        <td class="tab-highlighted-2" width="13"> 
          <div align="left">Pen #</div> 
        </td> 
        <td class="tab-highlighted-2" width="15"> 
          <div align="left">Zoo</div> 
        </td> 
        <td class="tab-highlighted-2" width="20"> 
          <div align="left">Remarks</div> 
        </td> 

    </tr> 

    <c:forEach items="${beans}" var="view"> 
        <tr> 

            <td><a href="/oosd/UpdateAnimal.jsp?animal_id=${view.animalId}&breed=${view.breedId}">${view.animalName}</a></td>  
            <td>${view.animalDob}</td>
            <td>${view.animalGender}</td>
            <td>${view.animalSource}</td>
            <td>${view.animalBreed}</td>
            <td>${view.sectionId}</td>
            <td>${view.zooId}</td>
            <td>${view.animalRemark}</td>

        </tr> 
    </c:forEach> 
</table> 

         </center>

</body></html>

AnimalUpdateBean.java 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;}


}

the servlet responsible to start the update is UpdateAnimalServlet:

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); 
}
       }
    }

I’m using a Relay servlet that calls AnimalUpdateServlet for execution:

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("=> 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:

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>content.LoginServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>UpdateAnimalServlet</servlet-name>
    <servlet-class>content.UpdateAnimalServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>SelectAnimalServlet</servlet-name>
    <servlet-class>content.SelectAnimalServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>Relay</servlet-name>
    <servlet-class>content.Relay</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SelectAnimalServlet</servlet-name>
    <url-pattern>/SelectAnimalServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>UpdateAnimalServlet</servlet-name>
    <url-pattern>/UpdateAnimalServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Relay</servlet-name>
    <url-pattern>/Relay</url-pattern>
  </servlet-mapping>
</web-app>

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.

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

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:

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;
}
}

Any idea where is my mistake here ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:14:58+00:00Added an answer on May 26, 2026 at 5:14 pm

    as specie_id is a number and that’s what the thrown exception is stating “Data type mismatch in criteria expression.” and because of this exception sendRedirect error is occuring as because of the SQLException data has been already written to the response.
    your query should be something like

      "update animal set aname = '"+aname+"' , gender = '"+gender+"', specie_id = "
                      +Integer.parseInt(breedid)+" , remark = '"+remark+"' where animal_id = '"+id+"'"
    

    same would be required if animal_id is also a number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am trying to create a stacked barplot using the following code: test
Hello i been trying to get a tokenizer to work using the boost library
Hello I am trying to do a SOAP request here is my code: #
hello I am using ubuntu on android(with chroot) I am trying to execute xev
Hello I'm trying to take backup from mysql command line client. I'm using mysqldump
Hello everyone i am trying to format the input number range with php number_format
hello friends i am trying to get a list of user id and want
Hello Guys I am trying to figure out why i am gettings this error
Hello , I am using arshaw fullcalendar v1.5.2 (only month view ) for property
Hello i have this code var queue = new BlockingCollection<int>(); queue.Add(0); var producers =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.