Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > closing java.sql.Statement's

Reply
Thread Tools

closing java.sql.Statement's

 
 
Blah Blah
Guest
Posts: n/a
 
      10-15-2003
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


 
Reply With Quote
 
 
 
 
david
Guest
Posts: n/a
 
      10-15-2003

"Blah Blah" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Blah Blah
Guest
Posts: n/a
 
      10-15-2003
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" <(E-Mail Removed)> wrote in message
news:T2gjb.768$(E-Mail Removed).. .
>
> "Blah Blah" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Closing popup window when closing parent window? =?Utf-8?B?Vk1J?= ASP .Net 3 02-15-2007 08:29 AM
Closing the doors 15 minutes before closing. doofus Computer Support 12 06-11-2005 08:20 AM
Any way to save tabs when closing firefox? Brent Firefox 3 10-12-2004 09:30 PM
Application Error when closing Mozilla Jim Sanchez Firefox 3 05-09-2004 05:16 PM
Closing child window WITHOUT closing parent thomas Javascript 0 10-23-2003 04:10 PM



Advertisments