ResultSet 何时关闭?

编程入门 行业动态 更新时间:2024-10-21 11:47:25
本文介绍了ResultSet 何时关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如果我不关闭 ResultSet 是否可以关闭它?我有一个 ResultSet is closed 异常,但我确定我没有在任何地方关闭 ResultSet .我所做的就是使用 ResultSet 来执行 SELECT 查询,然后我使用相同的 ResultSet 因为它是由这种方法调用的:

I want to know if ResultSet can be closed if I didn't close it ? I have a ResultSet is closed exception but I am sure I didn't close the ResultSet anywhere . What I do exactly is that I use the ResultSet to perform a SELECT query then I use the same ResultSet because it's called by this method :

public Object getValueAt( int row, int column ) throws IllegalStateException { // ensure database connection is available if ( !dbConnection.isConnectedToDatabase() ) throw new IllegalStateException( "Not Connected to Database" ); // obtain a value at specified ResultSet row and column try { getResultSet().absolute( row + 1 ); return getResultSet().getObject( column + 1 ); } // end try catch ( SQLException sqlException ) { System.out.println("Exception from here dude"); sqlException.printStackTrace(); } // end catch return ""; // if problems, return empty string object } // end method getValueAt

那么,另一个问题:有没有办法确保 ResultSet 被打开?

So , another question : Is there a way to ensure the ResultSet is opened ?

第三个问题:也许是因为我从不关闭 ResultSets .

Third question : Maybe the problem because I never close ResultSets .

关闭 ResultSet 有什么意义?

What's the point of closing ResultSet ?

这就是在名为 DBConnection 的类的构造函数中创建语句的方式:

Edit : That's how statement is being created inside the constructor of a class called DBConnection:

Class.forName(driver); // connect to database connection = DriverManager.getConnection(url, username, password); // create Statement to query database statement = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY ); //connection ok connectedToDatabase=true;

ResultSet 是在我想执行语句后创建的.

ResultSet is created later whenever I want to execute a statement.

推荐答案

直接来自 有关 ResultSet.close() 的文档:

立即释放此 ResultSet 对象的数据库和 JDBC 资源,而不是等待它自动关闭时发生.

Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

...

注意:当 Statement 对象关闭、重新执行或用于从多个结果的序列中检索下一个结果时,ResultSet 对象会被生成它的 Statement 对象自动关闭.

Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.

因此,如果您关闭了生成 ResultSet 的 Statement 已关闭,则会出现该异常.

So, if you closed the Statement that generated your ResultSet is closed, you get that exception.

另一个问题的答案:您不应该像那样从 ResultSet 中读取结果.执行选择并立即从 ResultSet 读取您需要的所有数据,关闭连接,然后您可以根据需要读取获取的数据.你真的不应该有一个外部资源/类调用你的 getValueAt 方法,你希望它仍然连接到数据库.连接可能因许多其他原因而终止,所以这不是解决办法.

Another question's answer: you shouldn't read results from a ResultSet like that. Perform the select an read all the data you need from the ResultSet at once, close the connection and then later you can read the fetched data as much as you want. You really shouldn't have an external resource/class calling your getValueAt method, which you expect to still be connected to the database. Connection may be terminated for many other reasons, so that's not the way to go.

第三个答案:以上已回答.

Third answer: answered above.

最后一个答案:显式释放资源,无需等到 Statement is 关闭.

Last answer: releasing resources explicitly, without waiting for it to be closed when Statement is.

更多推荐

ResultSet 何时关闭?

本文发布于:2023-10-18 10:57:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ResultSet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!