ROW handle (ResultSet resultSet, ROW object) throws SQLException;
For each row of a query result, pureQuery calls the handle() method to create the object that represents that row. pureQuery passes to the method the query results in an instance of java.sql.ResultSet, with the ResultSet's cursor positioned on the current row. The parameter object is null. If you are creating an implementation of RowHandler<ROW>, implement this method to create and return an object of type <ROW> that represents the row indicated by the cursor of the ResultSet instance.
If the ResultSet object is empty, or the last row of the ResultSet object has been read, the handle() method is not called.
In this example, the returned String object for each row in a query result contains a delimited series of string values of each column. In this very general, simple handler, no detailed knowledge is needed regarding the input ResultSet object.
package customHandlers;
import com.ibm.pdq.runtime.handlers.RowHandler;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class SimpleStringRowHandler implements RowHandler<String>
{
public SimpleStringRowHandler ()
{
delimiter = ", ";
}
public SimpleStringRowHandler (String delimiter)
{
this.delimiter = delimiter;
}
private int columnCount = -1;
private String delimiter;
String handle (ResultSet resultSet, java.lang.String object) throws SQLException
{
if (columnCount < 0)
columnCount = resultSet.getMetaData().getColumnCount();
StringBuffer myBuff = new StringBuffer();
if (columnCount > 0) {
myBuff.append(resultSet.getString(1));
for (int ii=2; ii<=columnCount; ii++) {
myBuff.append(delimiter);
myBuff.append(resultSet.getString(ii)));
}
}
return myBuff.toString();
} // handle
}
There are three ways in which you can specify RowHandler objects for annotated methods.
@Select(sql = "select * from employee where workdept = ?1.departmentNumber")
@Handler(rowHandler = customHandlers.SimpleStringRowHandler.class)
public Iterator<String> selectEmployeesInDepartment(DepartmentBean department);
A RowHandlerWithParameters implementation class must provide two handle() methods: one method with two arguments, and one method with three arguments. The two-argument signature can be an empty method.
@Select ("SELECT EVENT_NAME, EVENT_LOCATION FROM EVENTS" +
"WHERE EVENT_CODE = ? AND EVENT_DATE = ? AND EVENT_TIME = ?")
@Handlers (rowHandlerWithParameters = com.bigtickets.eventRowHandlerWithParameters)
List<Event> getEvents (eventCode,eventDate,eventTime);
Sql String
ResultSet
Object[] (contains the input parameters)
The handle method has the ability to access parameters that were passed to the SQL statement in the Object array.
When you specify a handler as a parameter, the parameter must be the last parameter in the method signature. If you specify a RowHandler together with a ParameterHandler as parameters, the handlers must be the last two parameters in the method signature.
@Select(sql = "SELECT * FROM employee where workdept = ?1.departmentNumber")
Iterator<String> selectEmployeesInDepartment(DepartmentBean department, \
customHandlers.SimpleStringRowHandler rowHandler);
Iterator<String> employees = face.selectEmployeesInDepartment(theDepartment, new customHandlers.SimpleStringRowHandler("\t"));
@Select(sql = "SELECT * FROM employee where workdept = ?1.departmentNumber")
<ROW> Iterator<ROW> selectEmployeesInDepartment(DepartmentBean department, RowHandler<ROW> rh);
Iterator<String> employees = face.selectEmployeesInDepartment(theDepartment, new customHandlers.simpleStringRowHandler("\t"));
List<EmployeeString> emp = db.queryList("select * from employee where workdept = ?1.departmentNumber",\
new customHandlers.SimpleStringRowHandler("\t"), theDepartment);