Example – Executing a simple query returning tabular data
|
//create a QueryConnector for executing queries:
QueryConnector queryConnector = new QueryConnector();
//build a query:
BusinessQuery query1 = buildBusinessQuery();
//create a list of queries to execute:
List queries = Collections.singletonList(query1);
//execute the queries:
Map results = queryConnector.retrieveQueryResultMap(queries);
//fetch the result for the query1 from the map:
ResultSetInterface genericResultSet = (ResultSetInterface) results.get(query1.getID());
//extract the Java SQL ResultSet contained in the generic wrapper:
//(casting it since we know that query1 will return a java.sql.ResultSet
instance)
java.sql.ResultSet sqlResultSet = (java.sql.ResultSet) genericResultSet.getResultSet();
//loop through the rows, printing the values of the first column:
while (sqlResultSet.next()) {
String firstColumnValue = sqlResultSet.getString(1);
System.out.println(firstColumnValue);
}
//close the result set just retrieved and the server connection:
int closeConnectionAndResultSet =
QueryConnector.CloseOptions.ALL_RESULTSET_OPTIONS
| QueryConnector.CloseOptions.ALL_CONNECTION_OPTIONS;
queryConnector.closeResources(getSessionContext(), closeConnectionAndResultSet);
|
The result of running this program will be for the values of the
first column to be printed on the console, e.g.
Joyce
Jane
Louise
Alice
Barbara
Carol
Judy
Janet
Mary
Thomas
James
John
Robert
Jeffrey
Alfred
Henry
Ronald
William
Philip
For the SAS classroom dataset if "name" is the first data item in the
query built.