The example method below sends three rows with three columns each. Note that exceptions are not caught in the example; the server logs any uncaught exceptions that are thrown in a method call:
public void send_rows (IntegerHolder ih) throws
JException, SQLException
{
// Declare the constant ’pi’
final double pi = 3.1414; // Create the metadata object.
JServerResultSetMetaData
jsrsmd = JContext.createServerResultSetMetaData();
// There will be 3 columns in the result set.
jsrsmd.setColumnCount(3);
// The first column has datatype INTEGER and name ’one’.
jsrsmd.setColumnType(1, Types.INTEGER);
jsrsmd.setColumnName(1, "one");
// The second column has datatype VARCHAR and name ’two’.
jsrsmd.setColumnType(2, Types.VARCHAR);
jsrsmd.setColumnName(2, "two");
// The third column has datatype DOUBLE and name ’three’.
jsrsmd.setColumnType(3, Types.DOUBLE);
jsrsmd.setColumnName(3, "three");
// Create the result set object.
JServerResultSet jsrs = JContext.createServerResultSet(jsrsmd);
// Position the cursor.
jsrs.next();
// First row values: 1, "first", pi
jsrs.setInt(1, 1);
jsrs.setString(2, "first");
jsrs.setDouble(3, pi);
// Send the row.
jsrs.next();
// Second row values: 2, "second", pi * 2
jsrs.setInt(1, 2);
jsrs.setString(2, "second");
jsrs.setDouble(3, pi * 2.0);
// Send the row.
jsrs.next();
// Third row values: 3, "third", pi * 3
jsrs.setInt(1, 3);
jsrs.setString(2, "third");
jsrs.setDouble(3, pi * 3.0);
// Send the row.
jsrs.next();
// Demarcate the end of the result set by calling done().
jsrs.done();
}
The fragment below shows client-side code to call the stub and print the rows to the console.
try {
ih = new IntegerHolder();
comp.send_rows(ih);
ResultSet rs = comp.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
StringBuffer row = new StringBuffer("");
for (int i = 1; i <= rsmd.getColumnCount(); i++)
{
row.append(rsmd.getColumnName(i));
if (i < rsmd.getColumnCount())
row.append("\t");
}
System.out.println(row);
while(rs.next())
{
row = new StringBuffer("");
for (int i = 1; i <= rsmd.getColumnCount(); i++)
{
row.append(rs.getString(i));
if (i < rsmd.getColumnCount())
row.append("\t");
}
System.out.println(row);
}
// Discard any remaining results.
while(comp.getMoreResults())
{
rs = comp.getResultSet();
}
}
catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
e.printStackTrace();
}