Ibm®purequery is a high-performance Java™ data access platform aimed primarily at simplifying the development and management of data access applications. It is made up of tools, APIs, and runtimes. PureQuery introduces two programming styles to help users access the database through a simple but powerful API. This article describes one of the styles, inline method programming style, and discusses how to use it to efficiently query and update databases. This article also explores some of the advantages and key features of using inline methods for programming styles.
Brief introduction
PureQuery introduces two programming styles for SQL execution: Inline method programming style and annotated method programming style. These programming styles improve the out-of-the-box support for storing and retrieving objects, such as Java beans and maps, simplifying Java data access development. Inline programming styles also support the use of customized user-defined results processing.
Annotated method style provides a method of data access based on annotations. In the annotated method style, the Sql/xquery string is defined in a pureQuery annotation (Annotation). These comments are placed on the method declarations in the user-defined interface. The code generator preprocess the interface, generating the implementation code for each annotated method. The generated implementation code uses the SQL statement defined in the PureQuery runtime to execute the comment. To learn more about the style of annotated methods, see part 1th or pureQuery documentation for this series.
The purpose of the PureQuery inline programming style is to reduce the number of repetitive tasks that are common when using JDBC to query or update a database. The inline method style introduces a well-defined set of effective APIs that are simpler and easier to use than JDBC. In inline style, you can create a sql/xquery statement inline in your code like a Java String object. This dynamically generated statement is passed as a String parameter to the PureQuery Data interface method. Inline style uses JDBC best practices and leverages database-specific APIs to improve performance. An example of inline method style is the use of batch updating and improved result set processing. In annotated method style, SQL must be defined at compile time, but the inline method is different, and it supports the dynamic creation and execution of SQL statements at run time. In an application, SQL is "inline" and visible, making it easier to spot errors and make corrections. Inline style also has a pluggable custom result processing for easy mapping of database columns.
The code fragment in table 1 illustrates the simplicity and ease of use of inline method styles.
Table 1. Code comparison between JDBC and PureQuery inline style
Jdbc
try {
SQL for Insert
String sqlins= "' INSERT INTO CUSTOMER ("
+ "NAME, COUNTRY, Street, City, province, ZIP,"
+ "Phone,info)"
+ "VALUES (?,?,?,?,?,?,?,?)";
Prepare the INSERT statement
PreparedStatement pstmt =
Con.preparestatement (Sqlins);
Setup parameters
Pstmt.setstring (1, "CustName");
Pstmt.setstring (2, "custcountry");
Pstmt.setstring (3, "Custstreet");
Pstmt.setstring (4, "custcity");
Pstmt.setstring (5, "custprovince");
Pstmt.setstring (6, "Custzip");
Pstmt.setstring (7, "Custphone");
Pstmt.setstring (8, "custInfo");
Execute the INSERT statement
Pstmt.execute ();
Close the prepared statement
Pstmt.close ();
SQL for SELECT
String Sqlsel = "Select Name, Country, Street,"
+ "Province,zip from customer where customer =?";
Prepare the SELECT statement
pstmt = Con.preparestatement (Sqlsel);
Set the Input parameter
Pstmt.setstring (1, "custcountry");
Execute SELECT statement
Pstmt.execute ();
Get the results and set values in Customer Bean
ResultSet result = Pstmt.getresultset ();
List<customer> custlist =
New Arraylist<customer> ();
while (Result.next ()) {
Customer cust = new Customer ();
Cust.name = result.getstring (1);
Cust.country = result.getstring (2);
Cust.street = result.getstring (3);
Cust.province = result.getstring (4);
Cust.zip = result.getstring (5);
Custlist.add (Cust);
}
}
catch (SQLException e) {
E.pringstacktrace ();
}