11.4. Executing Queries
Prev | Chapter 11. Query | Next

## 11.4. Executing Queries

    public Object execute ();
    public Object execute (Object param1);
    public Object execute (Object param1, Object param2);
    public Object execute (Object param1, Object param2, Object param3);
    public Object executeWithArray (Object[] params);
    public Object executeWithMap (Map params);

As evident from the examples above, queries are executed with one of the many `execute` methods defined in the `Query` interface. If your query declares between 0 and 3 parameters, you can use the `execute` version that takes the corresponding number of `Object` arguments; each argument should be set to the value of the corresponding declared parameter. For queries with more than 3 parameters, you can use the more generic `executeWithArray` and `executeWithMap` methods. See the `Query` interface Javadoc for details.

All `execute` methods declare a return type of `Object`, but they really return `Collection`s. The JDO specification only uses `Object` rather than `Collection` to enable vendors to use proprietary return types in certain cases, and to allow for future expansion of the interface.

    public void close (Object result);
    public void closeAll ();

Query results may hold on to data store resources; therefore, all results should be closed when they are no longer needed. You can close an individual query result with the `close` method, or all open results at once with `closeAll`.

| Note
Query results from Kodo JDO always implement the `java.util.List` interface. This allows you to access the results in any order, and supports the common need to only process a certain range of objects in the result set. Because Kodo JDO can be configured to use scrollable JDBC `ResultSet`s where appropriate, list elements that are never accessed will not affect performance. Combined with Kodo JDO's memory-sensitive data structures, scrollable result sets also allow you to efficiently iterate over huge data sets -- even when the entire data set could not possibly fit into memory at once. These scrollable results consume database resources, however, so if you have configured Kodo to use scrolling result sets you are strongly encouraged to close your query results when you are through with them. If they are not closed immediately, they will be closed when they are garbage collected by the JVM.

Example 11.9. Processing a Query Result

    PersistenceManager pm = ...;

    Query query = pm.newQuery (Employee.class, "salary > 50000");
    Collection employees = (Collection) query.execute ();
    try
    {
        for (Iterator itr = employees.iterator (); itr.hasNext ();)
            processEmployee ((Employee) itr.next ());
    }
    finally
    {
        query.close (employees);
    }

Prev | Up | Next
11.3. JDOQL | Home | 11.5. Query Compilation