Update functionality error

From: Mohammed Mazid (kadmazid_at_hotmail.com)
Date: 03/29/04


Date: 29 Mar 2004 01:57:37 -0800

Can anyone please help me here? Basically I have modified the source
code and understood it but when I update a record in the db using a
JSP, it gives me an error "The flight you selected does exist."

Althought there is not selection going on, instead it is entered, I
need it to match the flight in the db using the flightNo. I want all
the attributes to be changed apart from the flightNo itself as it is
it's associated details I need changing (i.e. flightdate, destination,
arrTime and depTime).

Below are the code snippets but did not include everything as all
details were not neded. Therefore I had put part of it. But
somewhere I am going wrong of "updating" a db record.

public class Flight
{
  String flightNo;
  Date flightDate;
  String destination;
  Time arrTime;
  Time depTime;

  Flight(String flightNo, Date flightDate, String destination, Time
arrTime, Time depTime)
  {
   this.flightNo = flightNo;
   this.flightDate = flightDate;
   this.destination = destination;
   this.arrTime = arrTime;
   this.depTime = depTime;
  }

  public String getFlight()
  {
    return flightNo;
  }
  public Date getFlightDate()
  {
    return flightDate;
  }
  public String getDestination()
  {
    return destination;
  }
  public Time getArrTime()
  {
    return arrTime;
  }
  public Time getDepTime()
  {
    return depTime;
  }
  public String toString()
  {
    return flightNo;
  }
}

public class FlightService
{
  private FlightDAO flightDataAccess;

  public FlightService()
  { flightDataAccess = new FlightDAO(); }

  public Flight getFlight(String flightNo)
  {
    Flight flight = null;

    try
    {
      flight = flightDataAccess.retrieve(flightNo);
    }
    catch (ObjectNotFoundException onfe) { flight = null; }

    return flight;
  }

  public Flight updateFlight(String flightNo, Date flightDate, String
destination, Time depTime, Time arrTime)
  {
   Flight flight = new Flight(flightNo, flightDate, destination,
          depTime, arrTime);

  // Perform the DB transaction
  flightDataAccess.update(flight);

  return flight;
  }
}

class FlightDAO
{
  FlightDAO() {}

  Flight retrieve(String flightNo)
         throws ObjectNotFoundException {

    // Assume everything is declared

    try
        {
      // Get a database connection
      connection = connectionPool.getConnectionNoWait();

      // Create SQL SELECT statement
      stmt = connection.prepareStatement(RETRIEVE_STMT);

      // Initialize statement and execute the query
      stmt.setString(1, flightNo);
      results = stmt.executeQuery();

      // Iterator over the query results
      while ( results.next() )
          {
        // We expect only one row to be returned
        num_of_rows++;
        if ( num_of_rows > 1 )
                {
          throw new SQLException("Too many rows were returned.");
        }

        // Create and fill-in the Flight object
        flight = new Flight(results.getString("FlightNo"),
                            results.getDate("FlightDate"),
                            results.getString("Destination"),
                            results.getTime("ArrivalTime"),
                            results.getTime("DepartureTime"));
      }

      if ( flight != null ) { return flight; }
      else { throw new ObjectNotFoundException(); }

    // Handle any SQL errors
    }
        catch (SQLException se)
        {
      throw new RuntimeException("A database error occured. " +
        se.getMessage());

//Some more catch statements and finally blocks exist.
  }

  private static final String RETRIEVE_STMT
    = "SELECT FlightNo, FlightDate, Destination, ArrivalTime,
DepartureTime FROM Flight WHERE FlightNo = ?";

  void update(Flight flight) {

    // Assume all declared

    PreparedStatement update_stmt = null;

    try {
      // Get a database connection
      connection = connectionPool.getConnectionNoWait();

      // Create SQL INSERT statement
      update_stmt = connection.prepareStatement(UPDATE_STMT);

      // Add data fields
      update_stmt.setDate(1, flight.flightDate);
      update_stmt.setString(2, flight.destination);
      update_stmt.setTime(3, flight.arrTime);
          update_stmt.setTime(4, flight.depTime);

      // Execute SQL INSERT statement
      update_stmt.executeUpdate();

    // Handle any SQL errors
    }
        catch (SQLException se)
        {
      throw new RuntimeException("A database error occured. " +
se.getMessage());

//Assume catch and finally block exist
      }
    }
  }

private static final String UPDATE_STMT
 = "UPDATE Flight SET FlightDate = ?, Destination = ?, ArrivalTime =
?, DepartureTime = ? WHERE FlightNo = ?";
}

//Assume all imports exist

public final class FlightServlet extends HttpServlet
{
  public void doPost(HttpServletRequest request,
HttpServletResponse response)
         throws IOException, ServletException {

    processRequest(request, response);
  }

  public void processRequest(HttpServletRequest request,
                             HttpServletResponse response)
         throws IOException, ServletException {
    // Declare the dispatcher for the View
    RequestDispatcher view = null;

    // Declare service and database variables
    FlightService flightSvc;
    Flight flight = null;

    // Create the status object and store i tin the request for use
    // by the 'Error Page' View (if necessary)
    Status status = new Status();
    request.setAttribute("status", status);

    // Extract HTML form parameters
    String flightNo = request.getParameter("flightNo");
            
        String dateString = request.getParameter("flightDate");
        Date flightDate = Date.valueOf(dateString);
                        
        String destination = request.getParameter("destination");

        String timeString = request.getParameter("depTime");
        Time depTime = Time.valueOf(timeString + ":00");

        String timeStr = request.getParameter("arrTime");
        Time arrTime = Time.valueOf(timeStr + ":00");

    // Assume there are if-else statements

    // Now delegate the real work to the FlightService object
    try
        {
      // Create the "flight service" object
      flightSvc = new FlightService();

      // Check if this flight object already exists
      flight = flightSvc.getFlight(flightNo);
      if ( flight != null )
          {
        status.addException(
            new Exception("The flight you selected already exists;"));
      }

      // If any of the above verification failed, then return the
      // 'Update Flight Form' View and return without proceeding with
the
      // rest of the business logic
      if ( ! status.isSuccessful() ) {
        view = request.getRequestDispatcher("updateform.jsp");
        view.forward(request, response);
        return;
      }

      // Add the flight to the data-file
      flight = flightSvc.updateFlight(flightNo, flightDate,
destination,
          depTime, arrTime);
      request.setAttribute("flight", flight);

      // The flight update process was successful,
      // forward to the 'Flight Updated' View
      view = request.getRequestDispatcher("thank_you.jsp");
      view.forward(request, response);

    // Handle any business logic errors
    } catch (Exception e) {
      status.addException(e);
      view = request.getRequestDispatcher("updateform.jsp");
      view.forward(request, response);
    }
  }
}

Can anyone please suggest where to change and what would be the
correct code fragment? I'd be really appreciated. Thanks for your
contribution.



Relevant Pages

  • Update functionality error
    ... it gives me an error "The flight you selected does exist." ... it's associated details I need changing (i.e. flightdate, destination, ... arrTime and depTime). ... String destination; ...
    (comp.lang.java.help)
  • Re: Airline Routes....
    ... peice of string and pull the string taut between SFO and LHR. ... This is he great circle route and the one the pilots will try to ... There aren't many geopolitical concerns between SFO and LHR. ... I've made the flight back and forth about six times and it's ...
    (rec.travel.air)
  • Re: Teaching the aerotow
    ... - In free flight, use ailerons to get the desired bank angle, your feet to keep the string in the middle and the elevator to keep the nose at the horizon. ... stay in position using all three controls as needed and when possible try to center the string. ... The theoretical reasons for how the glider reacts to control inputs can be interesting, but you don't need to know them to fly properly. ...
    (rec.aviation.soaring)
  • Re: YASID: `string of perls
    ... and the flight is boosted by laser lauched ... refueling, the fuel tanks being refered to as `a string of ...
    (rec.arts.sf.written)
  • Gibberish output is display when returning an array
    ... use String array instead as I can convert from a date/time to a ... // Return an array of flight details ... String aTimeString = String.valueOf; ...
    (comp.lang.java.programmer)