I am doing code review, and in many cases I found result is not closed if SQL statement returns the records. The business rule for SQL statement is consider only the first record.
Below is the code where I am confused, Why is it returning the value without closing resultset and statement? Is this correct way?
if (rs.next()){
return new Long(rs.getLong(1));
}
Below is sample code:
private static Long <MethodName>
(
oracle.sql.NUMBER[] o_errorCode,
oracle.sql.CHAR[] o_errorText,
Long portA,
Long portZ) throws SQLException
{
String errorMessage = "";
Long[] NROLE = new Long[1];
Long[] pTask = new Long[1];
Long dimObject = null;
Long objectIDA = null;
Long objectIDZ = null;
Long relation = null;
Connection tmpConn = null;
Statement stmt = null;
ResultSet rs = null;
String SQL = null;
try
{
// Retrieve the Epipe circuits that are on the specified ports
stmt = DbUtil.getConn().createStatement();
String query = "Select * from Circuit where ... Order by Id";
rs = stmt.executeQuery(query);
if (rs.next()){
return new Long(rs.getLong(1));
}
rs.close();
stmt.close();
return null;
}
catch (SQLException ex)
{
o_errorCode[0] = new oracle.sql.NUMBER(1);
o_errorText[0] = new oracle.sql.CHAR("SQLException - " + ex.getMessage(),
oracle.sql.CharacterSet.make(oracle.sql.CharacterSet.DEFAULT_CHARSET));
return(null);
}
catch (Exception e)
{
o_errorCode[0] = new oracle.sql.NUMBER(1);
o_errorText[0] = new oracle.sql.CHAR("Exception - " + e.getMessage(), oracle.sql.CharacterSet.make(
oracle.sql.CharacterSet.
DEFAULT_CHARSET));
return(null);
}
}