public SortedMap<String, TreeMap<String, String>> getCampo(String city, String sport, String
data) throws SQLException {
TreeMap<String, TreeMap<String, String>> campoInfo = new TreeMap<>();
Connection connection =null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
statement = connection.createStatement();
String query = String.format("SELECT * FROM CAMPO WHERE COMUNE='%s' AND SPORT='%s' AND DATA >= '%s' AND TORNEO = 0", city, sport, data);
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
String name = resultSet.getString("NOME");
String id = "" + resultSet.getInt("ID");
String comune = resultSet.getString("COMUNE");
String indirizzo = resultSet.getString("INDIRIZZO");
String desc = resultSet.getString("DESCRIZIONE");
String renter = "" + resultSet.getInt("RENTER");
String date = resultSet.getString("DATA");
String ora = resultSet.getString("ORA");
String metodo = resultSet.getString("METODODIPAGAMENTO");
String prezzo = resultSet.getString("PREZZO");
String isAffittabile = "" + resultSet.getString("AFFITTABILE");
TreeMap<String, String> info = new TreeMap<>();
info.put("ID", id);
info.put("COMUNE", comune);
info.put("INDIRIZZO", indirizzo);
info.put("DESC", desc);
info.put("RENTER", renter);
info.put("DATA", date);
info.put("ORA", ora);
info.put("PREZZO", prezzo);
info.put("METODODIPAGAMENTO", metodo);
info.put("AFFITTABILE", isAffittabile);
campoInfo.put(name, info);
}
}
catch (Exception e) {
// TODO: handle exception
}
finally {
if (resultSet!=null) {
resultSet.close();
}
if (statement!=null) {
statement.close();
}
}
if (connection!=null){
connection.close();}
return campoInfo;
}
I'm writing this function in Java with Sonar scanner. I have a problem with the variables 'statement' and 'resultSet', because when I try to close them, in the finally block, Sonar tells me that there's a bug and use try-with resources or close it in a finally block. It happens just for one of these variables, the second in the block. For example, first i close the 'resulSet' (and it's ok) but the second doesn't close and Sonar highlights it. What can I do?