我的代码应该是查询Derby数据库,但不是。更糟糕的是,它应该在单独的行上打印错误消息,但即使没有这样做。这是我的代码片段(我遇到问题):
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);
}
添加侮辱伤害的事实是MessageDialog在不应该出现时显示。
事实上,我得到这样的东西:
There was a problem processing your query.This error occurred on line 86
This error occurred on line 98
答案 0 :(得分:1)
您收到的错误消息是由于查询与数据库中的任何行都不匹配。他们的意思是:
更具体地说:
SELECT TripNumber,AverageSpeed FROM " +
"BikeTripRecords WHERE TripNumber=(SELECT count(TripNumber) FROM " +
"BikeTripRecords)
如果BikeTripRecord
的{{1}}等于所有旅行记录的计数,那么该查询只会返回一些内容。据推测,您正在尝试获取最后/最新记录。如果是这样,该查询充其量是“脆弱的”。如果旅行记录被删除或旅行编号序列中有“漏洞”,则查询可能会给您错误的记录或根本没有记录。
TripNumber
如果这个没有给你结果,那么该表是空的。
简而言之,您的问题似乎是您的查询和应用程序设计的问题,而不是Derby或Java。
我建议您查看数据库中实际存在的数据,并尝试使用Derby的查询工具执行这些查询。 (我假设它有一个...)
答案 1 :(得分:0)
这是查询。如果我会说SELECT Count(TripNumber)FROM BikeTripRecords之类的东西,我本来可以得到TripNumber,然后我应该说lastTripSpeed的SELECT AverageSpeed FROM BikeTripRecords WHERE TripNumber =(SELECT max(TripNumber)FROM BikeTripRecords)。我应该从代码中休息一下,回来,然后就会明白这一点。 D'哦!