Reusing resultset objects in Java

I'm working on a swing project I use JDBC ODBC connection bridge to access the data in SQL database I use the following code

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 the while loop, if I use the result set object again, it will throw an exception when the result set is closed Is there any other object other than the result set to get data from the database in disconnected mode

Solution

The result set does not contain results. It is an iterator in the results and is still in the database (or in the driver's cache)

So it's perfectly normal

If you want to keep the list of results after disconnecting, just copy them to a specific list, such as ArrayList This means that you must interpret the column at copy time (that is, use getInt or getString or getbinarystream and get the content, etc.)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>