Using data access objects to run SQL statements

You can query and update database objects in pureQuery by calling methods that you define in a custom interface.

After you define such an interface, you run the pureQuery Generator to create a class that implements the interface.

The com.ibm.pdq.runtime.factory.DataFactory class provides a means for your application to instantiate implementations of the interfaces in which you define your annotated methods. The implementation of the custom interface returned by the DataFactory.getData() method also implements com.ibm.pdq.runtime.Data.

When you instantiate an implementation of a custom interface so that you can run SQL against a database, you need to have an open connection to that database.

  1. You create a java.sql.Connection for the database.
  2. Then, you use the DataFactory.getData() method to instantiate an implementation that is connected to that database.

For example, you can instantiate an implementation with application logic similar to this:

Connection con = DriverManager.getConnection(...);
CustomerQuery cQuery = 
    DataFactory.getData(CustomerQuery.class, con);

When you instantiate an implementation of a custom interface so that you can run SQL against a data source, that data source must be open.

  1. You create a javax.sql.DataSource for the data source.
  2. Then, you use the DataFactory.getData() method to instantiate an implementation that is connected to that data source.

For example, you can instantiate an implementation with application logic similar to this:

import javax.naming.*; 
import javax.sql.*; 
... 
Context ctx=new InitialContext(); 
DataSource ds=(DataSource)ctx.lookup("...");

CustomerQuery cQuery = 
    DataFactory.getData(CustomerQuery.class, ds);

The DataFactory creates a java.sql.Connection, then instantiates a Data object which uses the Connection to access the underlying data store. If the application then needs the implicitly-created Connection, for example to modify a Connection property, it can acquire a reference to it by using the getConnection() method of the Data object.

Attention: Data objects are not thread safe. Do not share them between multiple threads. Use a Data object only in the same thread that it was created in.

Feedback