在 Java 程序中使用结果集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4016715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:28:37  来源:igfitidea点击:

Using Resultset in Java Program

javajdbcresultset

提问by LGAP

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.

使用上面的 java 代码,我从名为 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,

在尝试使用 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:

更新:

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));}

采纳答案by Bozho

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.

您必须将结果集的光标移动到一行 - byresultSet.first()或 by resultSet.next()。最初光标指向第一行之前,因此您的例外。

When you want to iterate the ResultSet:

当你想迭代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)idxis the column, not the row.

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

回答by mikej

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

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

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
}

回答by casablanca

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

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

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:

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

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 next3 times manually:

另一个更新:假设您的查询总是只返回 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);

回答by Sankar

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.

从 emp 中选择 count(*);通常,此查询将返回值 14。

so your java code

所以你的java代码

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

will return only one value i.e., 14

将只返回一个值,即 14

So, How can you access rs.getString(2). This will automatically throws an exception which you got in the second case.

那么,如何访问 rs.getString(2)。这将自动抛出您在第二种情况下遇到的异常。