Resultset rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");
使用上面的java代码,检索名为feedsCA的表中的行数。在Java程序中使用结果集
虽然试图检索使用rs.getInt计数(1),rs.getInt(2),rs.getInt(3),I与误差如以下说结束,
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetHasCurrentRow(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
at SimpleMail.main(SimpleMail.java:151)
UPDATE :
上述例外已解决。
但我得到以下异常,对此我不知道原因。请指教。
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
at SimpleMail.main(SimpleMail.java:152)
这就是我如何更新我的程序。找到我是一种合乎逻辑的方式,因为我可以很好地理解下面的循环将不会按需要工作。
rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");
while(rs.next()){
pw.printf(rowFormat, rs.getLong(1),"0",rs.getLong(2),rs.getLong(3));}
感谢Bozho!请更新我最近的例外情况。查看我更新的问题 – LGAP 2010-10-25 16:34:04
@LAPP:您的查询是'SELECT COUNT(*)...',它只返回1个值。因此唯一有效的索引是1,这就是为什么它抱怨索引2超出范围。 – casablanca 2010-10-25 16:35:24
@casablanca&Bozho请发现我更新的问题... – LGAP 2010-10-25 16:40:41