There are two ways to work with tables:
Method |
Advantage |
Using the standard Java ResultSet object |
Programming with standard methods of the Java language |
Using the SAP Java RFC ITable, IRow, and ICursor interfaces |
Allows you to use multiple cursors in a single table |
Using the ITable, IRow, and ICursor interfaces of Java RFC directly you can perform the same tasks as when using the java.sql ResultSet methods.
Note that not all of the java.sql ResultSet methods were implemented in Java RFC, because not all of the methods are applicable to working with RFC tables (See the list of excluded methods in the Java RFC HTML Reference).
However, the majority of the methods were implemented, and methods that were implemented use the Java RFC ITable, IRow, and ICursor interfaces.
Using ITable, IRow, and ICursor Directly
ITable
Tables are stored and accessed on a row-by-row basis.
ITable provides methods for specifying how the table is handled
ITable can create ICursor objects to navigate between rows.
ITable also includes a getResultSet method, which allows you to create an object implementing the standard Java (java.sql) ResultSet interface.
IRow
Each IRow object contains a reference back to the originating ITable object, from which the metadata information (ComplexInfo) of the fields can be obtained.
An IRow inherits all methods of IComplexField.
ICursor
ICursor is used for navigating the table, moving between various rows.
The ITable object can create an unlimited number of ICursor objects, each containing a reference back to the table.
If you do not specify the row to point to when creating a cursor, it points to the first row by default.
Note that cursors are not necessarily updated automatically when rows are inserted or deleted from a table. We recommend that you finish your work with all cursors before adding or deleting rows.
Row Buffering
Tables are managed on the client side through "smart-caching": no rows are shipped from the Orbix server to the client until the client requests a certain row. When the client requests a row, that particular row along with the next or previous few rows are cached on the client side. More rows are cached as the client accesses rows that are out of the current cache.
With the setFetchForward method you can specify the direction of the fetching of rows, that is, you can specify whether the rows after the requested row are fetched, or the rows before the requested row are fetched.
The size of the read buffer (in terms of number of rows included in it) for each cache operation is configurable and accessible through the methods setReadBufferSize and getReadBufferSize. You can specify no caching by setting the buffer size to 0. You can ask for the whole table to be cached by setting the buffer size to -1. The default buffer size is -1.
Row Updates
The getRow method of ITable and the various get methods of ICursor return a clone of a row in the table, not the row itself. As a result, any modifications to the returned row have no effect on the row of the originating table. To apply the changes to the originating table, call the updateRow method, passing to it the modified row copy.
Update Policies
You can control when updates of the table on the client side are sent back to the server, by specifying one of the following update policies:
Update Policy |
Time of Updates |
UPDATE_POLICY_BUFFERED (Default) |
All updates are cached on the client side until either acceptUpdate is called or until the next RFC call. |
UPDATE_POLICY_IMMEDIATE |
Any call to updateRow results in a call back to the server to synchronize with the update. |
The update policy can be set and retrieved through the methods setUpdatePolicy and getUpdatePolicy respectively. The default policy is UPDATE_POLICY_BUFFERED.
Examples Using ITable, IRow, and ICursor
The following examples show how to deal with two fields in the table parameter CUSTOMER_T (internal name: BRFCKNA1) of the RFC function RFC_CUSTOMER_GET. The two fields we show are KUNNR (customer number) and NAME1 (first line in customer name). Handling the two fields is shown as an example of how to handle all the fields in the row of the table.
Appending a Row to a Table
//Create the table
ITable tableFactory.autoCreateTable("CUSTOMER_T", "BRFCKNA1");
IRow theRow = theTable.createEmptyRow();
theRow.getSimpleField("KUNNR").setString(custNo);
theRow.getSimpleField("NAME1").setString(custName);
theTable.appendRow(theRow);
Updating a Row
ITable theTable = ...; //Create the table
IRow rowCopy = theTable.getRow(0); //get a copy of the first row
rowCopy.getSimpleField("KUNNR").setString(custNo);
rowCopy.getSimpleField("NAME1").setString(custName);
theTable.updateRow(0, rowCopy); // VERY IMPORTANT STEP!!!
Traversing a Table
ITable theTable = ...; //Create the table
//gets the number of columns for the table
int fieldCount = theTable.getComplexInfo().getCount();
ICursor cur = theTable.createCursor();
IRow theRow = cur.getCurrentRow(); //get a copy of the first row
while( null != theRow )
{
//prints out all fields in this row in the form of strings
for (int nColumn = 0; nColumn < nFieldCount; nColumn++)
{
System.out.print(theRow.getSimpleField(nColumn).getString());
System.out.print("\t");
}
System.out.print("\n");
theRow = cur.getNextRow(); //get a copy of the next row
}
Examples Using the Standard Java ResultSet Interface
Appending a Row to a Table
ITable theTable = ...; //Create the table
java.sql.ResultSet resultSet = theTable.getResultSet(false);
resultSet.afterLast();
resultSet.moveToInsertRow();
resultSet.updateString("KUNNR", custNo);
resultSet.updateString("NAME1", custName);
theTable.insertRow();
Updating a Row
ITable theTable = ...; //Create the table
java.sql.ResultSet resultSet = theTable.getResultSet(false);
resultSet.first(); //move to the first row
//this is equivalent to: resultSet.absolute(1);
resultSet.updateString("KUNNR", custNo);
resultSet.updateString("NAME1", custName);
resultSet.updateRow();
Traversing a Table
ITable theTable = ...; //Create the table
java.sql.ResultSet resultSet = theTable.getResultSet(false);
int fieldCount = resultSet.getMetaData().getColumnCount();
//gets the number of columns for the table
//upon creation, resultSet's internal cursor is positioned
//before the first row
while( resultSet.next() ) //move cursor to next row
{
//prints out all fields in this row in the form of strings
for (int nColumn = 1; nColumn <= nFieldCount; nColumn++)
{
System.out.print(resultSet.getString(nColumn));
System.out.print("\t");
}
System.out.print("\n");
}
See Also
See the Java RFC HTML Reference documentation for the details of each of the table-related interfaces.