Use try-with-resources or close this "PreparedStatement" in a "finally" clause

Hi,

Welcome to the community!

According to the javadoc the ResultSet.close method throws SQLException.

If your rs.close() call does that, then pst.close is never reached. So either you do this laborious exercise:

finally {
  try {
    if (rs != null){ 
      rs.close();
    }
  } finally {
    if (pst != null) {
// &etc
    }
  }
}

OR you create your objects with a try-with-resources (as demonstrated in the rule description, which is available from the issue box when you click “Why is this an issue”) and they’re closed automatically for you without all the rigmarole & if-ing.

 
HTH,
Ann