HowTo execute parameterized SQL using OracleAS TopLink 

OracleAS TopLink - How to execute parameterized SQL using OracleAS TopLink

Date: 12/04/2004

After completing this HowTo you should be able to:
• Understand how to execute parameterized SQL using OracleAS TopLink
• Run the sample using the instruction given

Table of Contents

Introduction
Prerequisites
Software Requirements
HowTo execute parameterized SQL using OracleAS TopLink
Useful References

Introduction 

Oracle Application Server TopLink is an advanced object-to-relational persistence. It helps to build high performance applications that store persistent data in a relational database. OracleAS TopLink supports parameterized SQL, which improves performance by reusing the same prepared statement for multiple executions.

Prerequisites 

Before we get started, the following are the assumptions that will be made in this document:
  • Minimum knowledge about JDBC, prepared statements, OracleAS TopLink.
  • Must have HR schema installed on the database to run the sample

Software Requirements 

HowTo execute parameterized SQL using OracleAS TopLink 

Parameterized SQL enables to create and store queries that are complete except for one or more bind parameters. These parameters are bound, while executing the query. Parameterized SQL avoids the prepare component of SQL execution and thus improves the performance of frequently executed SQL statements.

By default, OracleAS TopLink prints data inlined into its generated SQL and does not use parameterized SQL, because many JDBC drivers do not fully support parameter binding, and have size or type limits. Execution of Parameterized SQL can be enabled either for the whole session or for the individual query.

i) To enable parameterized SQL for the whole session

Set the should-bind-all-parameters and should-cache-all-statements in sessions.xml

<toplink-configuration>
<session>
...
<login>
...
<should-bind-all-parameters>true</should-bind-all-parameters>
<should-cache-all-statements>true</should-cache-all-statements>

</login>
</session>
</toplink-configuration>

Alternatively, this can also be done using the API shown like below :

 
// Get the session from session.xml named  'ParameterizedSQLSession'
DatabaseSession session =
(DatabaseSession)SessionManager.getManager().getSession("ParameterizedSQLSession");

session.getProject().getLogin().bindAllParameters();
session.getProject().getLogin().cacheAllStatements();

ii) To enable parameterized SQL for individual query

Using setShouldBindAllParameters() and setShouldCacheStatement() methods on individual queries, enables parameterized SQL execution. This causes OracleAS TopLink to use a prepared statement, binding all SQL parameters and caching the prepared statement. When the query is re-executed, the SQL preparation is avoided, which improves performance.


  ...
// Create expression builder.
ExpressionBuilder builder = new ExpressionBuilder();

// Create parameterized expression,
// 'salary' is the parameter to be bound while executing this query.

Expression exp = (builder.get("salary").greaterThan(builder.getParameter("salary")));
// Create parameterized query based on the above expression

ReadAllQuery query = new ReadAllQuery();
query.setReferenceClass(Employees.class);
query.setSelectionCriteria(exp);
query.addArgument("salary");

// enable parameter binding and prepared statement caching
query.setShouldBindAllParameters(true);
query.setShouldCacheStatement(true);

// create vector of values to bound to the query
Vector value = new Vector();
value.addElement("15000");
// execute the parameterized query
Vector results = (Vector) session.executeQuery(query,value);
...

Executing Parameterized RAW SQL

OralceAS TopLink supports bind parameters in RAW SQL. Bind parameters are specified using the hash character, '#'. The following code snippet shows how to execute parameterized RAW SQL.

  ...
DataReadQuery dq = new DataReadQuery();
dq.setSQLString("SELECT employee_id, first_name, salary FROM employees WHERE salary > #given_salary ");
dq.addArgument("given_salary");
dq.bindAllParameters();

// create vector of values to bound to the query
Vector value = new Vector();
value.addElement("15000");

// execute the parameterized query
Vector results = (Vector) session.executeQuery(dq,value);
...

Steps to run the sample 

  • Download the jar from here
  • Extract the jar file
  • Open <SAMPLE_HOME>/src/project.xml and set your database connection parameters in <connection-url>
  • Add toplink.jar, xmlparserv2.jar, connector.jar, classes12.jar and <SAMPLE_HOME>/src to the classpath
  • From <SAMPLE_HOME>/src directory compile the java source files
  • Run java oracle.otnsamples.toplink.parameterizedsql.ParameterizedSql

Useful References 

Please enter your comments about this sample in the OTN Sample Code Discussion Forum.


How-to execute parameterized SQL using OracleAS TopLink

Please rate this how-to:
Excellent
Good
Average
Below Average
Poor
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy