I've got the following code:
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(someSql)) {//stuff}
How do I check that connection is not null here?
Also, I got a method which returns a PreparedStatement like this:
private PreparedStatement getPreparedStatement(Connection connection)
throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setString(1, "someString");
return preparedStatement;
}
this gets called within a method that uses the following try with resources:
try (Connection connection = getConnection();
PreparedStatement preparedStatement =
getPreparedStatement(connection)) {//stuff}
Now I would assume the prepared statement will be autoclosed, because it gets initiated in the try with resources. But SonarCloud says that i should use try with resources in the getPreparedStatement-method or close that PreparedStatement in a finally block. Is this a wrong finding by SonarCloud or is there a better way to do it?