ResultSet什么时候关闭?

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

我想知道如果我没有关闭它,是否可以关闭ResultSet?我有一个ResultSet是关闭异常但我确信我没有关闭任何地方的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 ?

第三个问题:可能是问题因为我从未关闭ResultSet。

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.

...

注意:一个ResultSet当Statement对象关闭,重新执行或用于从多个结果序列中检索下一个结果时,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声明时等待它被关闭。

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

更多推荐

ResultSet什么时候关闭?

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

发布评论

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

>www.elefans.com

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