Using a View Object to Insert and Delete Rows in Code

The following code example contains three methods in addition to main:

package d2e;
import oracle.jbo.*;
import oracle.jbo.domain.Number;
public class RowsDemo {
public static void main(String[] args) {
    // Helper routine connects to the generic Application Module.
ApplicationModule appMod =
QueryDemo.getGenericAppMod(JboContext.PLATFORM_LOCAL);
    // Specify the Java file that defines the VO.
// Format: <package>
String voDefFile = "d2e.DeptView";
    // Identify the View Object. Must be a valid Java identifier.
String voName = "demoDeptVO";
    // Create the View Object within the context defined 
// by the Application Module.
ViewObject vo = appMod.createViewObject(voName, voDefFile);
addRow(appMod, vo);
deleteRow(appMod, vo);
}
  public static void addRow(ApplicationModule appMod, ViewObject vo) {
    // Create a row and fill in the columns.
Row newRow = vo.createRow();
newRow.setAttribute("Deptno", new Number(14));
newRow.setAttribute("Dname", "Pubs");
newRow.setAttribute("Loc", "Honolulu");
vo.insertRow(newRow);
    // Call a helper method.
commitAndShowChanges(appMod);
}
  public static void deleteRow(ApplicationModule appMod, ViewObject vo) {
    // Get row(s) to delete.
vo.setWhereClause(" deptno > 10 and deptno < 20 ");
    // Delete row(s).
vo.executeQuery();
while (vo.hasNext()) {
vo.next();
vo.removeCurrentRow();
}
// Call a helper method.
commitAndShowChanges(appMod);
  }
public static void commitAndShowChanges(ApplicationModule appMod) {
// Helper method to print updated table.
    try {
appMod.getTransaction().commit();
}
catch (Exception e) {
System.out.println("\n Could not commit changes.");
System.exit(0);
    }
// Define and execute a simple SQL statement.
String sqlStr = "select * from dept ";
String dumpClass = "oracle.jbo.server.QueryDumpTab";
String result = appMod.getTransaction().dumpQueryResult(sqlStr, dumpClass, null);
    // Print updated table.
System.out.println(result);
}
}

For more information on the dumpQueryResult() method, see Using Transaction Utility Methods.