• 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

Something wrong in returning ResultSet

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static ResultSet Query(String sql,Connection conn) throws Exception
{
Statement stmt = conn.createStatement();
ResultSet temprs;
temprs=stmt.executeQuery(sql);
return temprs;
}

Whats wrong while return ResultSet from a function.?
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you describe the error you are getting? I'm assuming you are getting some kind of an SQLException, but we'll need to know in order to diagnose the problem.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The biggest problem is that it becomes very difficult to clean up your resources. There are two separate issues.
First, you have to make sure that every place where this method is called, once the ResultSet is fully processed, that "close()" is called. If you are writing all of the code that access this method, then you have control over that. If you allow other people access to this method, you have to hope that they call "close()", but you have no direct control over it.
Second, even if you manage to call "close()" on every ResultSet that gets returned, how do you close the Statement (stmt)? Your "stmt" variable goes out of scope once the method returns, and so Java can do a little garbage collecting since there are no references to it, but there is no way you can call "close()" on it. So the database can/may/will have resources tied up that are never freed.
So it's a bad idea to do this.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic