This example uses the following simple definition of a table called HRDEPT.EMPLOYEE.
CREATE TABLE HRDEPT.EMPLOYEE(
EMPNO CHAR(6) NOT NULL,
FIRSTNME VARCHAR(12) NOT NULL,
MIDINIT CHAR(1),
LASTNAME VARCHAR(15),
WORKDEPT CHAR(2),
PHONENO CHAR(4),
HIREDATE DATE,
PRIMARY KEY(EMPNO))
A bean that corresponds to this table might look like this.
public Employee {
public String employeeId;
public String firstName;
public String middleInitial;
public String lastName;
public String departmentId;
public String extension;
public Date hireDate;
}
Instances of this bean provide the values in the records that the example code inserts into the table HRDEPT.EMPLOYEE. When you use beans to provide input parameters to SQL statements, the parameter markers in the SQL statements tell pureQuery which bean property provides the value of each parameter in the SQL. Therefore, even though the names of the properties in the bean and the columns in the table do not match, the definition of the bean does not need to include @Column annotations. If pureQuery created instances of this bean to hold the results of queries, the definition of the bean would require the @Column annotation for each property.
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.
The interface in which you define the annotated method for inserting a record might look like this:
import com.company.Employee;
public interface HumanResources
{
@Update(sql=
"INSERT INTO HRDept.Employee(EMPNO, FIRSTNME, MIDINIT, LASTNAME, WORKDEPT, PHONENO, HIREDATE)" +
"VALUES( :employeeId, :firstName, :middleInitial, :lastName, :departmentId, :extension, :hireDate)",
int newEmployee( Employee newHire );
}
The interface defines a method named newEmployee() as returning an int, which represents the SQL statement's update count, and making use of the single Employee object, newHire.
The interface's @Update annotation provides the SQL statement to run when the newEmployee() method is invoked.
After you generate an implementation of the HumanResources interface, you can use it in an application like this one:
Connection con = DriverManager.getConnection(...); 1
HumanResources hr = 2
DataFactory.getData( HumanResources.class, con );
Employee newCollegeHire = 3
new Employee("000010", "CHRISTINE", "I", "HAAS", "A00",
"3978", new java.sql.Date(System.currentTimeMillis()));
int oneCount = hr.newEmployee(newCollegeHire); 4
The code performs the following steps:
If you want the INSERT 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 update() 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
Employee newCollegeHire = 3
new Employee("000010", "CHRISTINE", "I", "HAAS", "A00",
"3978", new java.sql.Date(System.currentTimeMillis()));
int oneCount = db.update( 4
"INSERT INTO HRDept.Employee(EMPNO, FIRSTNME, MIDINIT, LASTNAME, WORKDEPT, PHONENO, HIREDATE) " +
"VALUES(:employeeId, :firstName, :middleInitial, :lastName, :departmentId, :extension, :hireDate )",
newCollegeHire );
The code performs the following steps: