Lets say I have the following code :
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.createStatement(myQueryString);
rs = ps.executeQuery();
// process the results...
} catch (java.sql.SQLException e) {
log.error("an error!", e);
throw new Exception("I'm sorry. Your query did not work.");
} finally {
ps.close(); // if we forgot to do this we leak
rs.close(); // if we forgot to do this we leak
}
and I wished to catch the scenario where I forget to close the PreparedStatement or the ResultSet using Checkstyles. Is this possible, and if so, how would I go about it?
PMD and Findbugs both have warnings for PreparedStatements (and ResultSets and Connections). I’d suggest using them for this type of warning, since CheckStyle has more to do with code style than finding data-flow bugs such as this.