closing java.sql.Statement's

B

Blah Blah

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
 
D

david

Blah Blah said:
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:
 
B

Blah Blah

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top