This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread 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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 29220
    
135

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: 2476
    
    7

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: 383

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: 2476
    
    7

Welcome to JavaRanch, Vijayakumar!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how is close() used?
 
Similar Threads
ResultSet Thru ResultSet
Making ResultSet unEditable
Making ResultSet unEditable
How long time can I keep a database connection open?
visual J++ and retreiving rows from sql server