Solved

JDBC related question

Posted on 2006-10-19
3
191 Views
Last Modified: 2010-03-31
If I use the same statement object and resultset object with more that one query like below
stmt = conn.prepareStatement(q1);
rs = stmt.executeQuery();
while(rs.next){
.....
.......
}
...
.....
.......//do I have to close the resultSet and the statement Objects before using them again.
stmt = conn.prepareStatement(q2)
rs=stmt.executeQuery()
.....
......
.......//close the same again in the same program?

My Question is if I want to use the same statement object and resultSet for more than one Query in the same program, do I have to close each time before using them again. If so, do I have to close the following each and every time in the program
1. ResultSet
2. Statement Object
3. Connection object
0
Comment
Question by:winsuncareer
  • 3
3 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 500 total points
ID: 17768189
> 1. ResultSet
You dont use the same ResultSet for another query, but you have to close it after you finished working with it

ResultSet.close()
Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.
Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results. A ResultSet object is also automatically closed when it is garbage collected.

> 2. Statement Object
No. closing is not requied to execute another query

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.


> 3. Connection object
No
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17768319
PreparedStatement can only execute the query it was created for (q1), so how do you want to use the same stmt for another query (q1) ?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17768329
errata --> another query (q2)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question