This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes Need to close statement object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Need to close statement object?" Watch "Need to close statement object?" New topic

Need to close statement object?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1062
When I create a query using a connection and returning a ResultSet via anexecuteQuery(), do I need to worry about closing the local variable statement object in the method that creates the ResultSet, IF I close the ResultSet in the calling method once I'm done with it?

It seems the answer is "no" since closing the statement object removes the results from the ResultSet.

Still, I wanted to see if I should do things in a different order or whatever.

Thanks in advance for any replies.

Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704

You should also close result set, statements, and connections (in that order) as soon as your are done with them since this allows the garbage collector to make optimum use of memory and connections.

In fact, most people will tell you that such things should be closed inside a finally block (connections in particular) to make absolutely sure your application does not accidentally leave connections open in cases where your application throws an exception.
[ September 18, 2007: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1062
Thanks very much for your reply!!!

Much appreciated.

Don't get me started about those stupid light bulbs.
subject: Need to close statement object?