在Java程序中使用结果集

发布于 2024-09-29 03:51:28 字数 1626 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半仙 2024-10-06 03:51:28

您必须将结果集的光标移动到一行 - 通过 resultSet.first() 或通过 resultSet.next()。最初,光标指向第一行之前,因此出现异常。

当您想要迭代ResultSet时:

while(rs.next()) {
    ...
}

更新:对于您的第二个问题 - (如卡萨布兰卡所述)您的查询似乎只返回一列,并且您要求第二个和第三个 - 但没有找到它们。请注意,在 rs.getX(idx) 中,idx 是列,而不是行。

You have to move the cursor of the result set to a row - either by resultSet.first() or by resultSet.next(). Initially the cursor is pointing before the first row, hence your exception.

When you want to iterate the ResultSet:

while(rs.next()) {
    ...
}

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.

丑疤怪 2024-10-06 03:51:28

您需要在访问第一行之前调用rs.next()

通常,您将像这样迭代结果集:

ResultSet rs = ...;
while (rs.next()) {
  ...
}

更新: 请注意,SELECT COUNT(*) ... 每行仅返回一个字段,即计数。您可能有几行,但每一行只有一个字段,该字段的索引为 1。您需要迭代这些行以获取所有计数:

while (rs.next()) {
  System.out.println(rs.getInt(1));
}

又一个更新: 假设您的查询将始终仅返回 3 行。但是,如果您绝对确定这一点,那么您可以手动调用 next 3 次:

long l1, l2, l3;
rs.next();
l1 = rs.getLong(1);
rs.next();
l2 = rs.getLong(1);
rs.next();
l3 = rs.getLong(1);
pw.printf(rowFormat, l1,"0",l2,l3);

You need to call rs.next() before accessing the first row.

Typically, you will iterate over the result set like this:

ResultSet rs = ...;
while (rs.next()) {
  ...
}

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:

while (rs.next()) {
  System.out.println(rs.getInt(1));
}

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:

long l1, l2, l3;
rs.next();
l1 = rs.getLong(1);
rs.next();
l2 = rs.getLong(1);
rs.next();
l3 = rs.getLong(1);
pw.printf(rowFormat, l1,"0",l2,l3);
笔落惊风雨 2024-10-06 03:51:28

在使用 getxxx 方法之前,您需要使用其中一种方法将 ResultSet 光标移动到一行。即rs.next()rs.first()rs.last()。如果已找到有效行,这些方法将返回 true,因此

if (rs.first()) {
  int field1 = rs.getInt(1); 
  // other columns
}

返回多行的查询的典型模式是 或 :

while (rs.next()) {
  int field1 = rs.getInt(1);
  // other columns
}

You need to use one of the methods to move the ResultSet cursor to a row before using the getxxx methods. i.e. rs.next(), rs.first() or rs.last(). These methods return true if a valid row has been located so a typical pattern is

if (rs.first()) {
  int field1 = rs.getInt(1); 
  // other columns
}

or for a query that returns multiple rows:

while (rs.next()) {
  int field1 = rs.getInt(1);
  // other columns
}
友谊不毕业 2024-10-06 03:51:28

据我所知,您的查询只会获得一行和一列,即查询返回的总行数。

举例来说:

Select count(*) from emp;
一般来说,这个查询将返回一个值14。

所以你的java代码

if(rs.next())
    rs.getInt(1); 

将只返回一个值,即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

if(rs.next())
    rs.getInt(1); 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文