Solved

JDBC related question

Posted on 2006-10-19
3
187 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
Comment Utility
> 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
Comment Utility
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
Comment Utility
errata --> another query (q2)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now