This example uses the following simple definition of a table called SALES.CUSTOMER.
CREATE TABLE SALES.CUSTOMER ( CUSTID BIGINT NOT NULL, NAME VARCHAR(24), ADDRESSL1 VARCHAR(50), CITY VARCHAR(24), REGION BIGINT, PRIMARY KEY(CUSTID))
A simple bean that corresponds to this table might look like this.
public Customer { public Integer custId; public String name; public String addressL1; public String city; public Integer region; }
When you use annotated methods to manipulate a database object, you must define an interface that defines those methods, use the pureQuery Generator to generate an implementation of that interface, and then write an application that calls the methods that are in the implementation class.
For a query to populate an iterator with Customer objects for customers that are in a particular sales region, you might define an interface like this:
import com.company.Customer; public interface CustomerQuery { @Select(sql= "SELECT * FROM Customer WHERE region=?1") Iterator<Customer> getCustomersInRegion(int r); }
The interface defines the getCustomersInRegion() method as returning an Iterator of com.company.Customer objects, based on a single int parameter that identifies the region where the customers are located.
The @Select annotation specifies the SQL query that populates the function's returned Iterator, based on equality between the region column of the CUSTOMER table and the function's first input parameter, which marked with ?1.
After you define the interface, you can run the pureQuery Generator to generate an implementation of that interface. The name of the implementation is CustomerQueryImpl. The package for the implementation is the package that contains the CustomerQuery interface.
After you generate an implementation of the CustomerQuery interface, you can use it in an application like this one:
Connection con = DriverManager.getConnection(...); 1 CustomerQuery cqcQuery = DataFactory.getData( CustomerQuery.class, con ); 2 Customer c; int r = 123; Iterator<Customer> customers = 3 cqcQuery.getCustomersInRegion(r); while (customers.hasNext()){ 4 c = customers.next(); System.out.println(c.custId+" "+c.name); } ((ResultIterator) customers).close(); 5
The code performs the following steps:
If you want the SELECT statement to be visible in the application's source, you can use the inline programming style.
In your application, you would call a version of the overloaded queryIterator() method that is defined in an implementation of the Data interface.
Your application might look like this:
Connection con = DriverManager.getConnection(...); 1 Data db = DataFactory.getData(con); 2 Customer c; int region = 123; Iterator<Customer> customers = db.queryIterator( 3 "SELECT custId, name FROM Customer WHERE region=?1", Customer.class, region); while (customers.hasNext()){ 4 c = customers.next(); System.out.println(c.custId+" "+c.name); } ((ResultIterator) customers).close(); 5
The code performs the following steps: