Skip Headers

# Specifying a Custom SQL String in a DatabaseQuery

Previous

Next

All `DatabaseQuery` objects provide a `setSQLString` method that you can use to define a custom SQL string.

For more information about using custom SQL in queries, see "Using SQL Calls".

Example: A Direct Read Query with SQL uses SQL to read all employee IDs.

A Direct Read Query with SQL

    DirectReadQuery query = new DirectReadQuery();
    query.setSQLString("SELECT EMP_ID FROM EMPLOYEE");
    Vector ids = (Vector) session.executeQuery(query);

Example: A Data Modify Query with SQL uses SQL to switch to a different database.

A Data Modify Query with SQL

    DataModifyQuery query = new DataModifyQuery();
    query.setSQLString("USE SALESDATABASE");
    session.executeQuery(query);

WARNING: Allowing an unverified SQL string to be passed into methods (for example: `setSQLString` method) makes your application vulnerable to SQL injection attacks.