473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Update functionality error

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
3 1996
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
10
by: Jim | last post by:
Hi, Let me explain how I'd generally do things. For a page where the user edits data, I'd generally call it something like editfred.php (for example), the page which updates the data would be...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
7
by: Jean-Marc Blaise | last post by:
Hi, The dev center calls sqlj.DB2_UPDATEJARINFO ('JMARC.JMB','JMB','file:JMB.sqlj') to update the sqlj routine source. I tried in CLP from the directory containing jar and sqlj files (Windows...
0
by: Todd Matson | last post by:
After installing a few security updates yesterday, I was surprised to discover that none of my MS Access applications work today. After doing some research, I have discovered that when the DAO...
4
by: Terry | last post by:
Hello, I am trying to get a response for an .aspx page in my current project (same virtual directory) by using WebRequest.GetResponse but I keep getting a exception with "500 Internal server...
9
by: Geraldine Hobley | last post by:
Hello I'm getting the above mentioned error in my applicatio I have a datagrid bound to a datasource like s MyDatagrid.DataSource = Mydataset.Tables(Order) - this all works fine However I...
8
by: mantrid | last post by:
Hello Im having problems working out why the following code does not work. I dont think its the sql as the error occurs on the first update which ever one is put there ($q1 or $q2). Ive swapped...
3
by: tarscher | last post by:
Hi all, I have a grid that contains 7 columns from 3 tables (3 unique keys, 4 normal fields). I show this 7 columns on the gridview. I now want to add edit and delete functionality. This should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.