This example shows how to do basic manipulation of data in a DataGraph object which you have created.
Using the simple DataGraph that was created during the task Using the Java Database Connectivity data mediator service for data access, some typical data manipulation follows.
List customersList = graph.getList("CUSTOMER"); Iterator i = customersList.iterator(); while (i.hasNext()) { DataObject customer = (DataObject)i.next(); List ordersList = customer.getList("CUSTOMER_ORDER"); Iterator j = ordersList.iterator(); while (j.hasNext()) { DataObject order = (DataObject)j.next(); System.out.print( customer.get("CUSTFIRSTNAME") + " "); System.out.println( order.get("ORDERDATE")); } }
i = customersList.iterator(); while (i.hasNext()) { DataObject customer = (DataObject)i.next(); if (customer.get("CUSTFIRSTNAME").equals("Will")) { customer.set("CUSTFIRSTNAME", "Matt"); } }
((DataObject) customersList.get(0)).delete();
DataObject newCust = graph.createDataObject("CUSTOMER"); newCust.setInt("CUSTID", 12345); newCust.set("CUSTFIRSTNAME", "Will"); newCust.set("CUSTLASTNAME", "Smith"); newCust.set("CUSTSTREETADDRESS", "123 Main St."); newCust.set("CUSTCITY", "New York"); newCust.set("CUSTSTATE", "NY"); newCust.set("CUSTZIPCODE", "12345"); newCust.setInt("CUSTAREACODE", 555); newCust.set("CUSTPHONENUMBER", "555-5555"); graph.getList("CUSTOMER").add(newCust);
mediator.applyChanges(graph);