Skip Headers

# Using Parameterized SQL and Statement Caching in a DatabaseQuery

Previous

Next

To enable the parameterized SQL on individual queries, use `DatabaseQuery` methods `bindAllParameters` and `cacheStatement`. This causes TopLink to use a prepared statement, binding all SQL parameters and caching the prepared statement. When you reexecute this query, you avoid the SQL preparation, which improves performance.

A Simple ReadObjectQuery with Parameterized SQL

    ReadObjectQuery query = new ReadObjectQuery(Employee.class);
    query.setShouldBindAllParameters(true);
    query.setShouldCacheStatement(true);

Alternatively, you can configure parameterized SQL and binding at the `Login` level for all queries (see "Configuring JDBC Options").

For more information about using parameterized SQL and binding for data access optimization, see "Parameterized SQL (Binding) and Prepared Statement Caching".

Note: For applications using a J2EE data source or external connection pool, you must configure statement caching in the J2EE server's data source–not in TopLink.