SQL中的问题,java中的ResultSet

时间:2022-07-09 11:52:43

How can I iterate ResultSet ? I've tried with the following code, but i get the error java.sql.SQLException: Illegal operation on empty result set.

如何迭代ResultSet?我尝试使用以下代码,但我得到错误java.sql.SQLException:对空结果集的非法操作。

 while ( !rs.isLast()) {
     rs.next();
     int id = rs.getInt("person_id");
     SQL.getInstance().getSt().execute("INSERT ref_person_pub(person_id) VALUES(" + id + ")");
}

Update: I've found the problem. I have used only one statement from the SQL singleton. When the statement is closed it can't be used again.

更新:我发现了问题。我只使用了SQL单例中的一个语句。语句关闭后,不能再次使用。

2 个解决方案

#1


11  

As per the JDBC tutorial:

根据JDBC教程:

resultSet = statement.executeQuery();
while (resultSet.next()) { 
    int id = resultSet.getInt("id");
    // ...
}

The ResultSet#next() moves the cursor forward one row from its current position and returns true if the new current row is valid. Thus, the while loop will stop automatically when there are no more rows.

ResultSet#next()将光标从其当前位置向前移动一行,如果新的当前行有效,则返回true。因此,当没有更多行时,while循环将自动停止。

If it is supposed to return zero or one row instead of multiple rows, then rather use if instead:

如果它应该返回零行或一行而不是多行,那么改为使用if:

resultSet = statement.executeQuery();
if (resultSet.next()) { 
    int id = resultSet.getInt("id");
    // ...
}

This way you have the opportunity to add an else.

这样您就有机会添加其他内容。

Update, that said and unrelated to the actual problem, I see more potential problems in your code: first, you seem to fire multiple queries which are dependent on each other. This can be done more efficient. Are you familiar with SQL Joins? Second, aren't you leaking JDBC resources? It look like that you're acquiring a statement, but not getting a handle of it so that you can properly close it after use. Please consult the before linked JDBC tutorial for a basic explanation how to work properly with JDBC code and this article for several basic kickoff examples how to use JDBC properly. Otherwise your application may crash sooner or later when the DB runs out of resources.

更新,说明与实际问题无关,我发现代码中存在更多潜在问题:首先,您似乎触发了多个相互依赖的查询。这可以更有效地完成。你熟悉SQL连接吗?第二,你是不是泄漏了JDBC资源?看起来你正在获取一个声明,但没有掌握它,以便你可以在使用后正确关闭它。有关如何正确使用JDBC代码的基本说明,请参阅之前链接的JDBC教程,本文将介绍如何正确使用JDBC的几个基本启动示例。否则,当数据库资源耗尽时,您的应用程序可能会迟早崩溃。

#2


2  

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

#1


11  

As per the JDBC tutorial:

根据JDBC教程:

resultSet = statement.executeQuery();
while (resultSet.next()) { 
    int id = resultSet.getInt("id");
    // ...
}

The ResultSet#next() moves the cursor forward one row from its current position and returns true if the new current row is valid. Thus, the while loop will stop automatically when there are no more rows.

ResultSet#next()将光标从其当前位置向前移动一行,如果新的当前行有效,则返回true。因此,当没有更多行时,while循环将自动停止。

If it is supposed to return zero or one row instead of multiple rows, then rather use if instead:

如果它应该返回零行或一行而不是多行,那么改为使用if:

resultSet = statement.executeQuery();
if (resultSet.next()) { 
    int id = resultSet.getInt("id");
    // ...
}

This way you have the opportunity to add an else.

这样您就有机会添加其他内容。

Update, that said and unrelated to the actual problem, I see more potential problems in your code: first, you seem to fire multiple queries which are dependent on each other. This can be done more efficient. Are you familiar with SQL Joins? Second, aren't you leaking JDBC resources? It look like that you're acquiring a statement, but not getting a handle of it so that you can properly close it after use. Please consult the before linked JDBC tutorial for a basic explanation how to work properly with JDBC code and this article for several basic kickoff examples how to use JDBC properly. Otherwise your application may crash sooner or later when the DB runs out of resources.

更新,说明与实际问题无关,我发现代码中存在更多潜在问题:首先,您似乎触发了多个相互依赖的查询。这可以更有效地完成。你熟悉SQL连接吗?第二,你是不是泄漏了JDBC资源?看起来你正在获取一个声明,但没有掌握它,以便你可以在使用后正确关闭它。有关如何正确使用JDBC代码的基本说明,请参阅之前链接的JDBC教程,本文将介绍如何正确使用JDBC的几个基本启动示例。否则,当数据库资源耗尽时,您的应用程序可能会迟早崩溃。

#2


2  

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