Я пытаюсь закрыть свой набор результатов, соединение и утверждение с помощью try / finally, но Sonar, похоже, это не нравится.Какую ошибку я делаю и почему они не закрываются?Спасибо.
public static List<String> findByName(String firstName, String lastName) throws SQLException {
/*Connects to table and contexts a statement to this connection. Creates the appropriate statement (query)
according to the first name and last name nulls, builds an array from the ResultSet that is created.*/
List<String> nameList = new ArrayList();
String query;
Connection conn = null;
Statement preparedStatement = null;
ResultSet allNames = null;
try {
conn = DriverManager.getConnection(getHost(), getuName(), getuPass());
preparedStatement = conn.createStatement();
if (firstName == null && lastName == null) {
query = "SELECT * FROM person_c";
} else if (firstName == null) {
query = "SELECT * FROM person_c WHERE LAST_NAME= '" + lastName + "'";
} else if (lastName == null) {
query = "SELECT * FROM person_c where FIRST_NAME= '" + firstName + "'";
} else {
query = "SELECT * FROM person_c where FIRST_NAME = '" + firstName + "'" + "AND LAST_NAME= '" + lastName + "'";
}
allNames = preparedStatement.executeQuery(query);
while (allNames.next()) {
nameList.add(allNames.getString("FIRST_NAME") + " " + allNames.getString("LAST_NAME"));
}
} finally {
if (allNames != null) allNames.close();
if (preparedStatement!=null) preparedStatement.close();
if (conn!=null) conn.close();
}
return nameList;
}