By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,346 Members | 2,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,346 IT Pros & Developers. It's quick & easy.

Update functionality error

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Mohammed Mazid" <ka******@hotmail.com> wrote in message
news:7c**************************@posting.google.c om...
// 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);
private static final String UPDATE_STMT
= "UPDATE Flight SET FlightDate = ?, Destination = ?, ArrivalTime =
?, DepartureTime = ? WHERE FlightNo = ?";
}
Can anyone please suggest where to change and what would be the
correct code fragment? I'd be really appreciated. Thanks for your
contribution.


Same thing you had wrong last time. You have five elements in your statement
waiting for data, and you only fill four of them.
Jul 17 '05 #2

P: n/a
But this is not a "missing element" error, I am indeed enter all of
them but it comes with an error saying "The flight object already
exists" from my knowledge means I am overwriting the existing flight
object. But where is it that I overwrite the flight object?

"Ryan Stewart" <zz********@gSPAMo.com> wrote in message news:<E9********************@texas.net>...
"Mohammed Mazid" <ka******@hotmail.com> wrote in message
news:7c**************************@posting.google.c om...
// 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);
private static final String UPDATE_STMT
= "UPDATE Flight SET FlightDate = ?, Destination = ?, ArrivalTime =
?, DepartureTime = ? WHERE FlightNo = ?";
}
Can anyone please suggest where to change and what would be the
correct code fragment? I'd be really appreciated. Thanks for your
contribution.


Same thing you had wrong last time. You have five elements in your statement
waiting for data, and you only fill four of them.

Jul 17 '05 #3

P: n/a
Sorry Ryan, I ignored the fourth thread from the previous one. Thanks for your tip.

"Ryan Stewart" <zz********@gSPAMo.com> wrote in message news:<E9********************@texas.net>...
"Mohammed Mazid" <ka******@hotmail.com> wrote in message
news:7c**************************@posting.google.c om...
// 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);
private static final String UPDATE_STMT
= "UPDATE Flight SET FlightDate = ?, Destination = ?, ArrivalTime =
?, DepartureTime = ? WHERE FlightNo = ?";
}
Can anyone please suggest where to change and what would be the
correct code fragment? I'd be really appreciated. Thanks for your
contribution.


Same thing you had wrong last time. You have five elements in your statement
waiting for data, and you only fill four of them.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.