在java中重用ResultSet对象

时间:2022-06-05 03:50:58

I am working on a swing project. In which i am using jdbc odbc Connection bridge to access the data from SQL database. I am using the following code

我正在做一个秋千项目。其中我使用jdbc odbc连接桥来访问SQL数据库中的数据。我使用以下代码

 try
  {
     Class.forName("sun.jdbc.odbc.JdbcodbcDriver");
     Connection con=DriverManager.getConnection("Jdbc:Odbc:dsn");
     Statement st=con.createStatement();
     ResultSet rs=st.executeQuery("select * from temp");
     int count c=0;
     while(rs.next())
     {
      c++;
     }

   }
  catch(Exception ex)        
   {
    ex.printStackTrace();
    }

In the above code after while loop, if i use again the result set object then it throws the exception as result set is closed . Is there any other object instead of result set to fetch the data from database in disconnected mode.

在while循环之后的上面的代码中,如果我再次使用结果集对象,那么它会在结果集关闭时抛出异常。是否有任何其他对象而不是结果集以在断开连接模式下从数据库中获取数据。

2 个解决方案

#1


9  

The result set doesn't contain the results, it's a kind of iterator over the results that still are in the database (or in a cache in the driver).

结果集不包含结果,它是结果的一种迭代器,仍然在数据库中(或在驱动程序的缓存中)。

So that's totally normal.

所以这是完全正常的。

If you want to keep a list of the results after you disconnected, just copy them to a concrete list before, for example an ArrayList. This means you'll have to do the interpretation of the columns (i.e. using getInt or getString or getBinaryStream and fetching the content, etc.) at the time of this copy.

如果要在断开连接后保留结果列表,只需将它们复制到具体列表中,例如ArrayList。这意味着您必须在复制时对列进行解释(即使用getInt或getString或getBinaryStream并获取内容等)。

#2


4  

Is there any other object instead of result set to fetch the data from database in disconnected mode.

是否有任何其他对象而不是结果集以在断开连接模式下从数据库中获取数据。

ResultSet gets the data after the connections has been established. And there is no other object to perform this task.

ResultSet在建立连接后获取数据。并且没有其他对象可以执行此任务。

You should store the data in some list ( or other data structure) and within while loop fill that list from ResultSet

您应该将数据存储在某个列表(或其他数据结构)中,并在while循环中填充ResultSet中的列表

#1


9  

The result set doesn't contain the results, it's a kind of iterator over the results that still are in the database (or in a cache in the driver).

结果集不包含结果,它是结果的一种迭代器,仍然在数据库中(或在驱动程序的缓存中)。

So that's totally normal.

所以这是完全正常的。

If you want to keep a list of the results after you disconnected, just copy them to a concrete list before, for example an ArrayList. This means you'll have to do the interpretation of the columns (i.e. using getInt or getString or getBinaryStream and fetching the content, etc.) at the time of this copy.

如果要在断开连接后保留结果列表,只需将它们复制到具体列表中,例如ArrayList。这意味着您必须在复制时对列进行解释(即使用getInt或getString或getBinaryStream并获取内容等)。

#2


4  

Is there any other object instead of result set to fetch the data from database in disconnected mode.

是否有任何其他对象而不是结果集以在断开连接模式下从数据库中获取数据。

ResultSet gets the data after the connections has been established. And there is no other object to perform this task.

ResultSet在建立连接后获取数据。并且没有其他对象可以执行此任务。

You should store the data in some list ( or other data structure) and within while loop fill that list from ResultSet

您应该将数据存储在某个列表(或其他数据结构)中,并在while循环中填充ResultSet中的列表