wood burning stoves 2.0*
The moose likes JDBC and the fly likes ResulSet issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ResulSet issue" Watch "ResulSet issue" New topic
Author

ResulSet issue

Gorakshanath Pandey
Greenhorn

Joined: Feb 25, 2004
Posts: 11
i have a sql query be executed up to thousand times, the only change
is the where clause value change, so each time i need to create a
statement to execute query, before create statement, do i need to close
last time opened resultset and statement ? Please advice.
Second Question is how to return a resultSet from a function ? Does the ResultSet Object gets destroyed once we exit the function or we need to
explicitly close it. The same is question for Statement Object.

temprs=SelQuery(SelStmt,con);
ResultSet dtrs;
String dtsql;
while(temprs.next())
{
String selopen="select count(*) from mast "
+" where reg_cd =" + "'" + temprs.getString("reg_cd ")+ "'"
+" and f_cmr = 'T' and closed_pms_dt between " + "'" + temprs.getDate("st_date") + "'" + " and " +"'" + temprs.getDate("ed_date") + "'";
Statement lstmt = con.createStatement();// Do i need to close this.
ResultSet ltemprs= lstmt.executeQuery(selopen); //Do i neet to close this
}

[ March 12, 2004: Message edited by: Bear Bibeault ]
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
do i need to close
last time opened resultset and statement
It is good form to always explicitly close you resources as soon as you are done with them.
Does the ResultSet Object gets destroyed once we exit the function or we need to
explicitly close it.
If the Statement is not closed, then the ResultSet will remain open as well. But if you close a Statement, any ResultSetS you may have created using that Statement will be automatically closed so you won't be able to use them.
Since you are executing the same query (except WHERE clause values), you should definitely consider using a PreparedStatement instead of a Statement. Most JDBC drivers support PreparedStatementS. You'll see better performance when using a PreparedStatement, especially if you are executing the same query 1000 times. PreparedStatementS also take care of that annoying quote-escaping problem.
You might want to try something like this:


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Gorakshanath Pandey
Greenhorn

Joined: Feb 25, 2004
Posts: 11
Thanks Blake...wonderful reply....Only one more question Is there
a way to return a ResultSet() Object from a function like

Public static void(?) Fn()
{
Statement stmt;
ResultSet rs
String Sql
...
...
rs=stmt.executeQuer(sql)

return rs; ///How to close this....
}
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
You would have to convert the ResultSet into domain objects, be sure to close it in the calling method, or use a javax.sql.RowSet
 
 
subject: ResulSet issue
 
Similar Threads
reusing the Statement object
ResultSet issue!!!!!!!!
Problem in Seperating Core Servlet to Database Query Java
ResultSet closing - regarding
Need to close statement object?