Example: Manipulating data in a DataGraph object
This example shows how to do basic manipulation of data in a DataGraph object which we have created.
Use the simple DataGraph created during the task Using the Java Database Connectivity data mediator service for data access, some typical data manipulation follows.
First get the list of customers, then for each customer get every order, then print out the customer’s first name and order date. (For this example, assume that you already know the last name is Smith).
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")); } }
Now change every customer with the name Will to be Matt.
i = customersList.iterator(); while (i.hasNext()) {DataObject customer = (DataObject)i.next(); if (customer.get("CUSTFIRSTNAME").equals("Will")) { customer.set("CUSTFIRSTNAME", "Matt"); } }
Delete the first Customer entry.
((DataObject) customersList.get(0)).delete();Add a new DataObject to the graph
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);Submit the changes.
mediator.applyChanges(graph);
Related tasks
Use the Java Database Connectivity data mediator service for data access