ResultSet什么时候关闭? [英] When is ResultSet closed?

查看:1128
本文介绍了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什么时候关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆