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

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

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

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,457
  2. Patrice
    Replies:
    3
    Views:
    7,350
    Michael D. Long
    Sep 22, 2004
  3. Michael D. Long

    Re: Opening / Closing SQL Server connections

    Michael D. Long, Sep 22, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    657
    Michael D. Long
    Sep 24, 2004
  4. thomson
    Replies:
    4
    Views:
    1,111
    Edwin Knoppert
    Dec 5, 2005
  5. =?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:
    916
    Thomas Hansen
    Feb 15, 2007
  6. ecoolone
    Replies:
    0
    Views:
    957
    ecoolone
    Jan 3, 2008
  7. weiwei

    asp and sql statement in sql server db

    weiwei, Sep 22, 2004, in forum: ASP General
    Replies:
    3
    Views:
    371
    Jeff Cochran
    Sep 22, 2004
  8. thomas
    Replies:
    0
    Views:
    434
    thomas
    Oct 23, 2003
Loading...