Solved

JDBC related question

Posted on 2006-10-19
3
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

626 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