I have code that is supposed to be querying a Derby database, but doesn't. Even worse, it is supposed to print the error messages on separate lines, but even fails to do that. Here is a snippet of my code (where I am having the problem):
try
{
connection = DriverManager.getConnection(DATABASE_URL);
//setting up the statement to handle our initial query
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
resultSet = statement.executeQuery("SELECT TripNumber,AverageSpeed FROM " +
"BikeTripRecords WHERE TripNumber=(SELECT count(TripNumber) FROM " +
"BikeTripRecords)");
//initializing maxShowableRecords depending on the number of BikeTrips already
//recorded to the database
if (!resultSet.next())
{
System.err.println("There was a problem processing your query.");
System.out.println("This error occurred on line 86");
}
else
{
tripCount = resultSet.getInt(1);
this.maxShowableRecords = (((x < tripCount) && (x > 1)) ? x :
tripCount);
lastTripSpeed = resultSet.getDouble(2);
}
//getting information about their trip history (and their last trip)
resultSet = statement.executeQuery("SELECT * FROM BikeTripAnalysis");
System.out.println("This error occurred on line 98");
if (!resultSet.next())
{
System.err.println("There was a problem processing your query.");
}
else
{
//storing the values in the view to variables so that we don't have to keep
//querying the database
minSpeed = resultSet.getDouble(1);
maxSpeed = resultSet.getDouble(2);
avgSpeed = resultSet.getDouble(3);
resultSet.close();
//forming the statement to display
messageToDisplay = String.format("Out of the %d trips you have " +
"recorded, these are your stats:\n\nMinimum Speed == " +
"%f\nMaximum Speed == %f\nAverage Speed == %.2f\n\nYour Last " +
"Speed == %.2f", tripCount, minSpeed, maxSpeed, avgSpeed,
lastTripSpeed);
if ((lastTripSpeed == minSpeed) && (tripCount > 1))
{
messageToDisplay += "Your last average speed was your worst. You " +
"can do better!!";
}
else
{
if ((lastTripSpeed == maxSpeed) && (tripCount > 1))
{
messageToDisplay += "\n\nYou have set a new speed record!! " +
"Keep on keeping on!!";
}
else
{
if (lastTripSpeed < avgSpeed)
{
messageToDisplay += "\n\nYou are below your average. Step" +
" it up!";
}
else
{
messageToDisplay += "\n\nYou are above your average. Keep." +
" It. Up!!";
}
}
//showing the user this message
JOptionPane.showMessageDialog(this, messageToDisplay);
}
}
}
catch (SQLException exception)
{
exception.printStackTrace();
System.exit(1);
}
Adding insult to injury is the fact that the MessageDialog is showing when it is NOT supposed to.
In fact, I get something like this:
There was a problem processing your query.This error occurred on line 86
This error occurred on line 98