Type a try-with-resources block opening Connection and PreparedStatement
Type a try-with-resources block opening Connection and PreparedStatement
Answer
try (Connection conn = DriverManager.getConnection(url, user, pass); PreparedStatement ps = conn.prepareStatement(sql)) { ps.executeUpdate(); }
Both Connection and PreparedStatement are AutoCloseable. try-with-resources closes ps first, then conn — reverse declaration order — even if an exception is thrown.