A
ast3r3x
I tried breaking out this chunk of code into a function so that I can
reuse it without having to have these huge try/catch blocks repeated
if I do multiple SQL queries.
So my problem is that the 'return' doesn't run in my first try block
because I have a finally block which supersedes returning anything.
However I can't return in the finally block because either a)it
returns and never closes the stmt and conn, or b)I have nothing to
return because stmt has always been closed.
Does anyone have any ideas how to return this ResultSet and still be
able to close it?
Possible ideas, but don't think they'll work:
1)Is there a way to pass by reference so that I can give this function
a ResultSet to pass the results to so I never have to return it?
2)If I locally make a ResultSet, and never perform .close() on it,
will it be deallocated on it's own since it's a local variable?
3)If this function was static, so that every time it ran, it would set
the same variable to the new results when it returns, would that stop
multiple ResultSets from not being closed?
/
************************************************************************/
public ResultSet sqlQuery(String sql)
{
Connection conn = null;
Statement stmt = null;
try
{
conn = RDBMServices.getConnection ();
stmt = conn.createStatement();
return stmt.executeQuery(sql);
}
catch (SQLException ex)
{
System.out.println("SQL Error 1: "+ex);
}
finally
{
try
{
if (stmt != null) stmt.close();
if (conn!= null)
{
RDBMServices.releaseConnection(conn);
}
}
catch (SQLException e) {}
}
}
/
************************************************************************/
reuse it without having to have these huge try/catch blocks repeated
if I do multiple SQL queries.
So my problem is that the 'return' doesn't run in my first try block
because I have a finally block which supersedes returning anything.
However I can't return in the finally block because either a)it
returns and never closes the stmt and conn, or b)I have nothing to
return because stmt has always been closed.
Does anyone have any ideas how to return this ResultSet and still be
able to close it?
Possible ideas, but don't think they'll work:
1)Is there a way to pass by reference so that I can give this function
a ResultSet to pass the results to so I never have to return it?
2)If I locally make a ResultSet, and never perform .close() on it,
will it be deallocated on it's own since it's a local variable?
3)If this function was static, so that every time it ran, it would set
the same variable to the new results when it returns, would that stop
multiple ResultSets from not being closed?
/
************************************************************************/
public ResultSet sqlQuery(String sql)
{
Connection conn = null;
Statement stmt = null;
try
{
conn = RDBMServices.getConnection ();
stmt = conn.createStatement();
return stmt.executeQuery(sql);
}
catch (SQLException ex)
{
System.out.println("SQL Error 1: "+ex);
}
finally
{
try
{
if (stmt != null) stmt.close();
if (conn!= null)
{
RDBMServices.releaseConnection(conn);
}
}
catch (SQLException e) {}
}
}
/
************************************************************************/