我在做代碼審查,並且在很多情況下,如果SQL語句返回記錄,我發現結果未關閉。 SQL語句的業務規則僅考慮第一條記錄。Java返回SQL語句
下面是我困惑的代碼,爲什麼它返回的值沒有關閉結果集和語句?這是正確的方法嗎?
if (rs.next()){
return new Long(rs.getLong(1));
}
下面是示例代碼:
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);
}
}
如何管理'finally'塊資源收? –