Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default 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
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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).

  3. #3
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    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.

Similar Threads

  1. SQL Query resultset into Excel Sheet
    By chandpuri in forum JDBC
    Replies: 8
    Last Post: 01-24-2011, 06:53 PM
  2. Error When Executing Query
    By radz in forum New To Java
    Replies: 5
    Last Post: 06-26-2008, 03:37 AM
  3. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 03:57 AM
  4. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 07:55 AM
  5. Query with 1 record vs. multiple records
    By anderma8 in forum JDBC
    Replies: 5
    Last Post: 07-09-2007, 11:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •