Arshad Rehmani
Arshad Rehmani

Reputation: 2185

Using Try With Resource

I have a try catch block as below. I am trying to convert it into a Try-with-resource. But I'm unable to do so because I can't write normal statements in newer try

        Connection connection = null;
        PreparedStatement preparedItemsStatement = null;
        ResultSet rs = null;
        String id = null;
        try {
            connection = getConnection();
            preparedItemsStatement = connection.prepareStatement(myQuery);
            preparedItemsStatement.setString(1, userId + "%");
            rs = preparedItemsStatement.executeQuery();
            if (rs != null && rs.next())
                id = rs.getString("SOME_ID");
        } catch (SQLException e) {
            throw new SQLException("Error running Database query ", e);
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (preparedItemsStatement != null)
                    preparedItemsStatement.close();
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                throw new SQLException("Error running Database query ", e);
            }
        }

What I Tried,

            try (
                Connection connection = getConnection();
                PreparedStatement preparedItemsStatement = connection.prepareStatement(myQuery);                
                preparedItemsStatement.setString(1, userId + "%");
                ResultSet rs = preparedItemsStatement.executeQuery();                 
            ) {           

            if (rs != null && rs.next())
                id = rs.getString("SOME_ID");
        } catch (SQLException e) {
            throw new SQLException("Error running Database query ", e);
        }

Upvotes: 1

Views: 84

Answers (2)

Titus
Titus

Reputation: 22474

You can do something like this:

try (Connection connection = getConnection();) {           
    try(PreparedStatement preparedItemsStatement = connection.prepareStatement(myQuery);){
        preparedItemsStatement.setString(1, userId + "%");
        try(ResultSet rs = preparedItemsStatement.executeQuery();){
             if (rs != null && rs.next()){
                 id = rs.getString("SOME_ID");
             }
        }
    }
} catch (SQLException e) {
    throw new SQLException("Error running Database query ", e);
}

Keep in mind that the resource you've opened this way will be closed as soon as the execution exits that try block.

Upvotes: 2

M A
M A

Reputation: 72854

Split them into two try-with-resources:

try (
     Connection connection = getConnection();
     PreparedStatement preparedItemsStatement = connection.prepareStatement(myQuery);
    ) {
        preparedItemsStatement.setString(1, userId + "%");
        try (ResultSet rs = preparedItemsStatement.executeQuery()) {
           ...

The statements inside the try block must be statements that declare variables of type AutoCloseable.

Upvotes: 5

Related Questions