wood burning stoves 2.0*
The moose likes JDBC and the fly likes Something wrong in returning ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Something wrong in returning ResultSet" Watch "Something wrong in returning ResultSet" New topic

Something wrong in returning ResultSet

Gorakshanath Pandey

Joined: Feb 25, 2004
Posts: 11
static ResultSet Query(String sql,Connection conn) throws Exception
Statement stmt = conn.createStatement();
ResultSet temprs;
return temprs;

Whats wrong while return ResultSet from a function.?
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
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.

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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.
It is sorta covered in the JavaRanch Style Guide.
subject: Something wrong in returning ResultSet
Similar Threads
how to call a stored procedure which do not return a resultSet
Resultset question
Please help with the resultset problem
Close a resultset in a function while returning the values
SQLException: No ResultSet