在Java程序中使用结果集
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)
更新:
上述异常有已解决。
但我得到以下异常,我不知道原因。请指教。
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));}
Resultset rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");
Using the above java code above, am retrieving the counts of rows from the table named feedsCA.
While trying to retrieving the counts using rs.getInt(1),rs.getInt(2),rs.getInt(3), I end with an error saying as below,
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:
The above exception has been resolved.
But I get the following exception, for which I dont know the reason. Please advise.
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)
This is how I have updated my program. Find me a logical way as I can understand well that the loop below will not work as required.
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));}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须将结果集的光标移动到一行 - 通过
resultSet.first()
或通过resultSet.next()
。最初,光标指向第一行之前,因此出现异常。当您想要迭代
ResultSet
时:更新:对于您的第二个问题 - (如卡萨布兰卡所述)您的查询似乎只返回一列,并且您要求第二个和第三个 - 但没有找到它们。请注意,在
rs.getX(idx)
中,idx
是列,而不是行。You have to move the cursor of the result set to a row - either by
resultSet.first()
or byresultSet.next()
. Initially the cursor is pointing before the first row, hence your exception.When you want to iterate the
ResultSet
:Update: For your second problem - (as noted by Casablanca) your query seems to return only one column, and you are asking for a 2nd and 3rd - and they are not found. Note that in
rs.getX(idx)
idx
is the column, not the row.您需要在访问第一行之前调用
rs.next()
。通常,您将像这样迭代结果集:
更新: 请注意,
SELECT COUNT(*) ...
每行仅返回一个字段,即计数。您可能有几行,但每一行只有一个字段,该字段的索引为 1。您需要迭代这些行以获取所有计数:又一个更新: 假设您的查询将始终仅返回 3 行。但是,如果您绝对确定这一点,那么您可以手动调用
next
3 次:You need to call
rs.next()
before accessing the first row.Typically, you will iterate over the result set like this:
Update: Note that
SELECT COUNT(*) ...
returns only one field per row, which is the count. You may have several rows, but each row will have only one field, which has index 1. You need to iterate through the rows to get all the counts:Yet another update: It's bad to assume that your query will always return only 3 rows. However, if you are absolutely sure of this, then you can just call
next
3 times manually:在使用
getxxx
方法之前,您需要使用其中一种方法将ResultSet
光标移动到一行。即rs.next()
、rs.first()
或rs.last()
。如果已找到有效行,这些方法将返回true
,因此返回多行的查询的典型模式是 或 :
You need to use one of the methods to move the
ResultSet
cursor to a row before using thegetxxx
methods. i.e.rs.next()
,rs.first()
orrs.last()
. These methods returntrue
if a valid row has been located so a typical pattern isor for a query that returns multiple rows:
据我所知,您的查询只会获得一行和一列,即查询返回的总行数。
举例来说:
Select count(*) from emp;
一般来说,这个查询将返回一个值14。
所以你的java代码
将只返回一个值,即14
那么,你如何访问rs.getString(2)。这将自动引发您在第二种情况下遇到的异常。
As far as my knowledge, your query will only get one row and column i.e., the total number of rows that your query returns.
Say for example :
Select count(*) from emp;
Generally this query will return a value 14.
so your java code
will return only one value i.e., 14
So, How can you access rs.getString(2). This will automatically throws an exception which you got in the second case.