-1

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
4

2 回答 2

1

The error messages you are getting are due to queries not matching any rows in the database. They mean that either:

  • your queries are wrong, or
  • the programs / processes that are inserting / updating the data are wrong, or
  • the data you are inserting is wrong.

More specifically:

    SELECT TripNumber,AverageSpeed FROM " +
            "BikeTripRecords WHERE TripNumber=(SELECT count(TripNumber) FROM " +
            "BikeTripRecords)

That query will only return something if there is a BikeTripRecord whose TripNumber is equal to the count of all trip records. Presumably you are trying to fetch the last / latest record. If so, that query is at best "fragile". If trip records are deleted or there is a "hole" in the trip number sequence, then the query might give you the wrong record or no record at all.

    SELECT * FROM BikeTripAnalysis

If this one gives you no results, then the table is empty.


In short, your problems seem to be problems with your queries and your application design, and not with either Derby or Java.

I suggest that you look at what data you've actually got in the database, and try executing those queries using Derby's query tool. (I assume it has one ...)

于 2013-07-13T02:43:37.123 回答
0

It was the query. If I would have said something like SELECT count(TripNumber) FROM BikeTripRecords, I could have gotten the TripNumber, and then I should have said SELECT AverageSpeed FROM BikeTripRecords WHERE TripNumber = (SELECT max(TripNumber) FROM BikeTripRecords) for the lastTripSpeed. I should have taken a break from the code, came back, and then it would have been this obvious. D'oh!

于 2013-07-13T03:12:48.457 回答