closing java.sql.Statement's

Discussion in 'Java' started by Blah Blah, Oct 15, 2003.

  1. Blah Blah

    Blah Blah Guest

    hi there,

    i'm trying to figure out when and where i have to close my
    java.sql.Statement objects. i have the following code:

    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
    conn = ds.getConnection();
    stmt = conn.createStatement();

    rset = stmt.executeQuery("select * from foo");
    while (rset.next()) {}
    rset.close();
    rset = null;

    /**** do i need this code?
    stmt.close();
    stmt=null;
    stmt=conn.createStatement();
    ****/

    rset = stmt.executeQuery("select * from bar");
    rset.close();
    rset = null;
    } catch (SQLException sqle) {
    } finally {
    try {if (rset != null) rset.close();} catch (SQLException e) {}
    try {if (stmt != null) stmt.close();} catch (SQLException e) {}
    try {if (conn != null) conn.close();} catch (SQLException e) {}
    }

    the question is - do i need to close my Statement, and create a new one,
    after every query? or can i just create one statement, and leave it alone
    until i'm done with everything?

    daniel
    Blah Blah, Oct 15, 2003
    #1
    1. Advertising

  2. Blah Blah

    david Guest

    "Blah Blah" <> wrote in message
    news:...
    > hi there,
    >
    > i'm trying to figure out when and where i have to close my
    > java.sql.Statement objects. i have the following code:
    >
    > Connection conn = null;
    > Statement stmt = null;
    > ResultSet rset = null;
    > try {
    > conn = ds.getConnection();
    > stmt = conn.createStatement();
    >
    > rset = stmt.executeQuery("select * from foo");
    > while (rset.next()) {}
    > rset.close();


    //I don't think you need:
    > rset = null;
    >
    > /**** do i need this code?


    //probably don't need:
    > stmt.close();


    //don't need:
    > stmt=null;


    //probably don't need:
    > stmt=conn.createStatement();
    > ****/
    >
    > rset = stmt.executeQuery("select * from bar");
    > rset.close();
    > rset = null;
    > } catch (SQLException sqle) {
    > } finally {
    > try {if (rset != null) rset.close();} catch (SQLException e) {}
    > try {if (stmt != null) stmt.close();} catch (SQLException e) {}
    > try {if (conn != null) conn.close();} catch (SQLException e) {}
    > }
    >
    > the question is - do i need to close my Statement, and create a new one,
    > after every query? or can i just create one statement, and leave it alone
    > until i'm done with everything?
    >
    > daniel
    >
    >
    david, Oct 15, 2003
    #2
    1. Advertising

  3. Blah Blah

    Blah Blah Guest

    the reason i ask is that i've been very careful about closing my
    Connection's and ResultSet's, but only close my Statement's after i'm done
    with them (after 1-10 queries/updates). and after a while my connection
    pool runs dry...


    "david" <> wrote in message
    news:T2gjb.768$...
    >
    > "Blah Blah" <> wrote in message
    > news:...
    > > hi there,
    > >
    > > i'm trying to figure out when and where i have to close my
    > > java.sql.Statement objects. i have the following code:
    > >
    > > Connection conn = null;
    > > Statement stmt = null;
    > > ResultSet rset = null;
    > > try {
    > > conn = ds.getConnection();
    > > stmt = conn.createStatement();
    > >
    > > rset = stmt.executeQuery("select * from foo");
    > > while (rset.next()) {}
    > > rset.close();

    >
    > //I don't think you need:
    > > rset = null;
    > >
    > > /**** do i need this code?

    >
    > //probably don't need:
    > > stmt.close();

    >
    > //don't need:
    > > stmt=null;

    >
    > //probably don't need:
    > > stmt=conn.createStatement();
    > > ****/
    > >
    > > rset = stmt.executeQuery("select * from bar");
    > > rset.close();
    > > rset = null;
    > > } catch (SQLException sqle) {
    > > } finally {
    > > try {if (rset != null) rset.close();} catch (SQLException e) {}
    > > try {if (stmt != null) stmt.close();} catch (SQLException e) {}
    > > try {if (conn != null) conn.close();} catch (SQLException e) {}
    > > }
    > >
    > > the question is - do i need to close my Statement, and create a new one,
    > > after every query? or can i just create one statement, and leave it

    alone
    > > until i'm done with everything?
    > >
    > > daniel
    > >
    > >

    >
    >
    Blah Blah, Oct 15, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dna
    Replies:
    1
    Views:
    1,270
  2. William \(Bill\) Vaughn
    Replies:
    0
    Views:
    442
    William \(Bill\) Vaughn
    Aug 21, 2003
  3. David Browne
    Replies:
    0
    Views:
    455
    David Browne
    Aug 21, 2003
  4. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    649
    Thomas Hansen
    Feb 15, 2007
  5. thomas
    Replies:
    0
    Views:
    225
    thomas
    Oct 23, 2003
Loading...

Share This Page