Solved

JDBC related question

Posted on 2006-10-19
3
188 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 80
compre toata in where clue oracle 4 56
github account with ecipse 1 42
java.io.BufferedReader   readLine()  //returns the entire file ? 4 59
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

910 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

22 Experts available now in Live!

Get 1:1 Help Now