Resultset rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");
使用上面的 java 代码,我从名为 feedsCA 的表中检索行数。
在尝试使用 rs.getInt(1)、rs.getInt(2)、rs.getInt(3) 检索计数时,我以如下错误结束:
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));}