A data service's perform operation accepts so-called requests which are composed of activities. These activities are capabilities or operations that the service can perform. Some of them can be stringed together so one activity uses the output of another for further processing. A particular data service may only support a subset of all available activities - this depends on the implementation.
In the client toolkit, all activity classes extend a common class uk.org.ogsadai.client.toolkit.activity.Activity and can be composed to form a request. This page shows you how to run simple SQL and XPath queries against a database.
In this example you will run a simple SQL query across a MySQL database and print out the results. For example, select * from littleblackbook where id='3475' will select a single row from the table littleblackbook.
SQLQuery query = new SQLQuery("select * from littleblackbook where id='3475'");
Response response = service.perform( query );
String responseString = response.getAsString();The response is an XML document which contains a result element for each activity. Each result describes the status of its activity (COMPLETED, ERROR, UNSTARTED) and holds data that the activity may have produced. In this example, these are the results of the SQL query in WebRowSet format.
See examples/tutorials/clienttoolkit/SimpleSQLQueryExample.java for an example solution.
When you have more than one query, you can wrap a number of operations into one request.
SQLQuery query1 = new SQLQuery("select * from littleblackbook where name like '%Krause'"); SQLQuery query2 = new SQLQuery("select * from littleblackbook where name like '%Sugden'");
Request request = new ActivityRequest( new SQLQuery[] { query1, query2 } );
ActivityRequest request = new ActivityRequest(); SQLQuery query = new SQLQuery("select * from littleblackbook where id = '2359'"); request.add( query );
Response response = service.perform( request );
See examples/tutorials/clienttoolkit/SQLQuerySequence.java for an example solution.
This example demonstrates how to evaluate a parameterised SQL query repeatedly against the same data resource, changing the parameter values for each iteration.
ActivityRequest request = new ActivityRequest();
SQLQuery query = new SQLQuery("select * from littleblackbook where id>? and id<?");The ? represent SQL parameters as usual.
query.setParameter(1, String.valueOf(i)); query.setParameter(2, String.valueOf(i + 20));The first argument is the position of the parameter in the SQL string (starting with 1) and the second argument is the value of the parameter.
request.add( query );
See examples/tutorials/clienttoolkit/ParameterisedSQLQuery.java for an example solution.
If you have access to an XML database, you can perform XPath queries using the XPathQuery object. The request is performed as before. For example, the XPath expression /entry[@id<10] selects all entry elements with an attribute id whose value is less than 10.
XPathQuery query = new XPathQuery( "/entry[@id<10]" );
See examples/tutorial/clienttoolkit/SimpleXPathQueryExample.java for an example.
Back: The Basics | Up: Using the Client Toolkit | Next: Processing Results |
© International Business Machines Corporation, 2004. | © The University of Edinburgh, 2004. |