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 how is close() used? 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 "how is close() used?" Watch "how is close() used?" New topic
Author

how is close() used?

Stephan Ort
Greenhorn

Joined: Jun 08, 2010
Posts: 5
hi i'm wondering how the close() method is used exactly.

which one is correct, when you want to make two queries?:

1)


2)


3)


which of these make all of the possible closings, or does none?

are there easier ways to use one Statement objects for more than one query although all possible closings are done? Is this even possible?

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30356
    
150

None. You should be closing the result set before the statement.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stephan Ort
Greenhorn

Joined: Jun 08, 2010
Posts: 5
thanks, so it would be:

Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

No. Now you are closing the resultset before you finished using it.
Jeanne was saying that you should close the resultset before closing the statement.

We advise you to close the jdbc resources in a finally block.


OCUP UML fundamental and ITIL foundation
youtube channel
Vijayakumar Rengaraj
Greenhorn

Joined: Jun 09, 2010
Posts: 5
In between the two DB interactions you can close the resultset but not the statement object .

But it is not advisable to close the resultset and statement objects within try block.
you can try the below code:

finally{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
}
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

you should close the resultset before closing the statement object relating it.

must make sure that you are done manipulating the resultset before closing it.



http://plainoldjavaobject.blogspot.in
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

Welcome to JavaRanch, Vijayakumar!
 
wood burning stoves
 
subject: how is close() used?