• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ResultSet is Closed - Help! :-(

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program that is retrieving data from a database into a ResultSet. The result set is then read into a Vector to be passed back to the calling method.

After much tinkering i have failed to solve the ResultSet is Closed error that keeps being generated, it is simply driving me mad!

I'm hoping that one of you can help me from going insane.

My code is below:



Thanks in advance for your help!
[ March 30, 2007: Message edited by: Emile Ghazzawi ]
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Emile,

- Are you able to find out at which line of the code you get the result set closed exception? Please provide the stack trace, if present.

- Are the methods of the class you have given called from some other class? If so, can you provide that code?

-Manhar.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule of thumb is to close the resultset when you don't need it.

public Vector<Object> openData(int num) throws SQLException, ClassNotFoundException
{
database();
res2 = statement.executeQuery("SELECT * FROM DATA WHERE A = " + num);

rows2 = new Vector<Object>();

while (res2.next())
{
row2 = new Vector<Object>();

row2.addElement(res.getInt(1));
row2.addElement(res.getString(2));
row2.addElement(res.getString(3));
row2.addElement(res.getString(4));
row2.addElement(res.getDouble(5));
row2.addElement(res.getDouble(6));
row2.addElement(res.getDouble(7));
row2.addElement(res.getString(8));

rows2.addElement(row2);
}
link.close();

return rows2;
}



Pay attention at the resultset, do you spot the difference?
 
Emile Ghazzawi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Vu Lee - i totally missed it. I had used a different ResultSet but forgot to update the rest of the code to point to it.

You were very diligent in spotting that, more so than me! :-)

Thanks once again!

Emile
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the design reason for that error is that you declared the ResultSet variables to be instance variables of the class, whereas they should really have been local variables of the methods that use them.

If you had made them local variables you would not have had this problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic