Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ResultSet is Closed - Help! :-(

 
Emile Ghazzawi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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 ]
 
Manhar Puri
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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.
 
vu lee
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic