Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2008, 08:15 AM
Member
 
Join Date: Jul 2008
Posts: 3
RadhaJayalakshmi is on a distinguished road
Can i use the same statement and resultset objects for executing multiple query?
hi,
i just want to know whether, i can use the same statement and resultset objects for execuing multiple query? The scenario is like this

try{
// Get the connection object
//Declare resultset
//declare statement

String queryA="select * from xyz";
String queryB = "select * from abc";

//Executing first query
stmt = conn.createStatement();
rs = stmt.executeQuery(queryA);

*******************
// DO I NEED TO CLOSE THE STATEMENT AND RESULTSET EXPLICITLY BEFORE RE-USING IT?
rs.close();
stmt.close();
*******************

//Executing the second query
***************************
//SINCE I HAVE CLOSED THE STATEMENT, I AM CREATING IT AGAIN
stmt=conn.createStatement();
**************************
stmt = conn.createStatement();
rs = stmt.executeQuery(queryA);


}catch(Exception e){
// some code here
}finally{
//code to close statement, resultset, connection
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-18-2008, 10:22 AM
Senior Member
 
Join Date: Jun 2008
Posts: 442
masijade is on a distinguished road
No, you don't need to close it (you probably should close the rs though, wrapping it in a separate try/catch block just for all eventuallities).
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-18-2008, 03:13 PM
Member
 
Join Date: Jul 2008
Posts: 31
skaspersen is on a distinguished road
You can re-use the statement, you do however want to close the resultset as soon as you are done with it

If your application makes use of the DB regularly I would also hold on to the connection and only close it when the application closes.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error When Executing Query radz New To Java 5 06-26-2008 05:37 AM
SQL Query resultset into Excel Sheet chandpuri Database 6 06-05-2008 04:08 PM
Can I store multiple objects in an array lareauk New To Java 9 05-29-2008 05:57 AM
Can I use vectors to store multiple types of objects Nathand Advanced Java 6 04-28-2008 09:55 AM
Query with 1 record vs. multiple records anderma8 Database 5 07-10-2007 01:33 AM


All times are GMT +3. The time now is 01:39 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org