This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how is close() used?  RSS feed

 
Stephan Ort
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Sheriff
Posts: 37384
531
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None. you should be closing the result set before the statement.
 
Stephan Ort
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, so it would be:

 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vijayakumar Rengaraj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Vijayakumar!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!