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