The following code example updates values in a database table by setting attributes of a Business Component. The example uses the generic Application Module and calls Row.setAttribute to set attributes.
package d2e;
import oracle.jbo.*;
import oracle.jbo.server.*;
import oracle.jbo.domain.Number;
public class SetAttrDemo {
public static void main(String[] args) {
// Helper routine connects to the generic Application Module.
ApplicationModule appMod =
QueryDemo.getGenericAppMod(JboContext.PLATFORM_LOCAL);
// Specify the XML file that defines the View Object.
// Format: <package>
String voDefFile = "d2e.EmpView";
// Identify the View Object. Must be a valid Java identifier.
String voName = "demoEmpVO";
// Create the View Object within the context defined
// by the Application Module.
ViewObject vo = appMod.createViewObject(voName, voDefFile);
Number newVal = new Number(6543);
if (updateAttr(vo, newVal)) {
try {
// Commit changes to the database, making
// updated data available to other Application Modules.
appMod.getTransaction().commit();
System.out.println("\n Transaction committed. \n");
}
catch (JboException ave) {
// Handle Entity-level validation exceptions here.
System.out.println("\n (Entity) Invalid value: " + newVal);
throw ave;
}
}
}
public static boolean updateAttr(ViewObject vo, Object newVal) {
boolean res = false;
// Optional: specify a WHERE clause to narrow the query.
vo.setWhereClause("EMPNO = 7369");
// Execute the query, set new attribute value. Only one row
// should be returned, so use vo.first().
vo.executeQuery();
while (vo.hasNext()) {
Row row = vo.first();
try {
row.setAttribute("Sal", newVal);
}
catch (JboException ave) {
// Handle Attribute-level validation exceptions here.
System.out.println("\n (Attribute) Invalid value: " + newVal);
throw ave;
}
res = true;
}
return res;
}
}