Java-ready statement in try-with-resources not working

Yesterday, several people at Stack recommended using try-with-resources. I do this for all database operations. Today I want to change Statement to PreparedStatement to make queries more secure. But when I try to use the prepared statement in try-with-resources, I keep getting errors, such as "expected identifier" or ";"; or ')'.

What am I doing wrong? Or is it impossible? This is my code:

    try (Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
        PreparedStatement stmt = conn.prepareStatement("SELECT id FROM users WHERE id = ? LIMIT 1");
        stmt.setInt(1, user);
        ResultSet rs = stmt.executeQuery()) {

        // if no record found
        if(!rs.isBeforeFirst()) {
           return false;
        }
        // if record found
        else {
            return true;
        }

    } catch (SQLException e) {
        // log error but dont do anything, maybe later
        String error = "SQLException: " + e.getMessage() + "\nSQLState: " + e.getSQLState() + "\nVendorError: " + e.getErrorCode();
        return false;

    }
+4
source share
3 answers

try-with-resource (Autoclosable). Connection, PreparedStatement ResultSet Autoclosable, .

stmt.setInt(1, user) , . ( ) try-with-resource!

. try-with-resource!

try (Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS)) {
    executeStatement(conn);
} catch (SQLException e) {
    // log error but dont do anything, maybe later
    String error = "SQLException: " + e.getMessage() + "\nSQLState: " + e.getSQLState() + "\nVendorError: " + e.getErrorCode();
    return false;
}

private void executeStatement(Connection con) throws SQLException {
    try (PreparedStatement stmt = conn.prepareStatement("SELECT id FROM users WHERE id=? LIMIT 1")) {
        stmt.setInt(1, user);
        try (ResultSet rs = stmt.executeQuery()) {
            // process result
        }
    }
}

( , SQL , . , , PreparedStatement, -resource. ).

+7

:

try (Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS)) {
     PreparedStatement stmt = conn.prepareStatement("SELECT id FROM users WHERE id = ? LIMIT 1");

        stmt.setInt(1, user);
        ResultSet rs = pstmt.executeQuery())

        // if no record found
        if(!rs.isBeforeFirst()) {
           return false;
        }
        // if record found
        else {
            return true;
        }

    } catch (SQLException e) {
        // log error but dont do anything, maybe later
        String error = "SQLException: " + e.getMessage() + "\nSQLState: " + e.getSQLState() + "\nVendorError: " + e.getErrorCode();
        return false;

    }

, - , try ()

+1

stmt.setInt(1, user);
ResultSet rs = stmt.executeQuery()

... try{ /*HERE*/ }

This is due to the fact that stmt- the created resource try (/*HERE*/) {}that will be usedtry{ /*HERE*/ }

Try-with-resources

try (/*Create resources in here such as conn and stmt*/)
{
  //Use the resources created above such as stmt
}

The fact is that everything that is created in the resource creation block implements AutoClosable, and when the block comes out try, is called on them close(). There is stmt.setInt(1, user);no resource in your code AutoCloseable, so the problem.

+1
source

All Articles