Product Documentation

c-treeDB API for Java

Previous Topic

Next Topic

Manage

Manage() provides data management functionality for your application and/or process.

Below is the code for Manage():

//

// Manage()

//

// Populates table and perform a simple query

//

static void Manage() throws IOException, CTException {

System.out.println("MANAGE");

// populate the tables with data

Add_CustomerMaster_Records();

Add_ItemMaster_Records();

Add_Transactions();

// display the orders and their items

Display_CustomerOrders();

Display_OrderItems();

}

//

// Add_CustomerMaster_Records()

//

// This function adds records to table CustomerMaster from an

// array of Strings

//

public static class CUSTOMER_DATA {

// struct members

public String number, zipcode, state, rating, name, address, city;

// struct constructor

public CUSTOMER_DATA(String number, String zipcode, String state, String rating, String name, String address, String city) {

this.number = number;

this.zipcode = zipcode;

this.state = state;

this.rating = rating;

this.name = name;

this.address = address;

this.city = city;

}

};

static void Add_CustomerMaster_Records() throws IOException {

CUSTOMER_DATA[] data = new CUSTOMER_DATA[4];

data[0] = new CUSTOMER_DATA("1000", "92867", "CA", "1", "Bryan Williams", "2999 Regency", "Orange");

data[1] = new CUSTOMER_DATA("1001", "61434", "CT", "1", "Michael Jordan", "13 Main", "Harford");

data[2] = new CUSTOMER_DATA("1002", "73677", "GA", "1", "Joshua Brown", "4356 Cambridge", "Atlanta");

data[3] = new CUSTOMER_DATA("1003", "10034", "MO", "1", "Keyon Dooling", "19771 Park Avenue", "Columbia");

int nRecords = data.length;

Delete_Records(recordCustMast);

System.out.println("\tAdd records in table CustomerMaster...");

try {

// start a transaction

MySession.Begin();

for (int i = 0; i < nRecords; i++) {

recordCustMast.Clear();

// populate record buffer with data

recordCustMast.SetFieldAsString(0, data[i].number);

recordCustMast.SetFieldAsString(1, data[i].zipcode);

recordCustMast.SetFieldAsString(2, data[i].state);

recordCustMast.SetFieldAsString(3, data[i].rating);

recordCustMast.SetFieldAsString(4, data[i].name);

recordCustMast.SetFieldAsString(5, data[i].address);

recordCustMast.SetFieldAsString(6, data[i].city);

// add record

recordCustMast.Write();

}

// commit transaction

MySession.Commit();

} catch (CTException E) {

Handle_Exception(E);

}

}

//

// Add_ItemMaster_Records()

//

// This function adds records to table ItemMaster from an

// array of Strings

//

public static class ITEM_DATA {

// struct members

public int weight;

public BigDecimal price;

public String itemnum, description;

// struct constructor

public ITEM_DATA(int weight, BigDecimal price, String itemnum, String description) {

this.weight = weight;

this.price = price;

this.itemnum = itemnum;

this.description = description;

}

};

static void Add_ItemMaster_Records() throws IOException {

ITEM_DATA[] data = new ITEM_DATA[4];

data[0] = new ITEM_DATA(10, new BigDecimal("19.95"), "1", "Hammer");

data[1] = new ITEM_DATA(3, new BigDecimal("9.99"), "2", "Wrench");

data[2] = new ITEM_DATA(4, new BigDecimal("16.59"), "3", "Saw");

data[3] = new ITEM_DATA(1, new BigDecimal("3.98"), "4", "Pliers");

int nRecords = data.length;

Delete_Records(recordItemMast);

System.out.println("\tAdd records in table ItemMaster...");

try {

// start a transaction

MySession.Begin();

for (int i = 0; i < nRecords; i++) {

recordItemMast.Clear();

// populate record buffer with data

recordItemMast.SetFieldAsInt(0, data[i].weight);

recordItemMast.SetFieldAsBigDecimal(1, data[i].price);

recordItemMast.SetFieldAsString(2, data[i].itemnum);

recordItemMast.SetFieldAsString(3, data[i].description);

// add record

recordItemMast.Write();

}

// commit transaction

MySession.Commit();

} catch (CTException E) {

Handle_Exception(E);

}

}

//

// Delete_Records()

//

// This function deletes all the records in the table

//

static void Delete_Records(CTRecord record) throws IOException {

boolean found;

System.out.println("\tDelete records...");

try {

// write lock required for transaction updates

record.Lock(LOCK_MODE.WRITE);

// start a transaction

record.Begin();

// read first record

found = record.First();

while (found) // while records are found

{

// delete record

record.Delete();

// read next record

found = record.Next();

}

// commit transaction

record.Commit();

// free locks

record.Unlock();

} catch (CTException E) {

Handle_Exception(E);

}

}

//

// Add_Transactions()

//

// Add an Order and associated Items "as a transaction" to their

// respective tables. A transaction is committed or aborted if the

// customer number on the order is confirmed valid. Likewise each

// item in the order is verified to be a valid item. SavePoints are

// established as an order is processed, allowing a transaction to

// rollback to the previously verified item

//

public static class ORDER_DATA {

// struct members

public Calendar orderdate, promdate;

public String ordernum, custnum;

// struct constructor

public ORDER_DATA(Calendar orderdate, Calendar promdate, String ordernum, String custnum) {

this.orderdate = orderdate;

this.promdate = promdate;

this.ordernum = ordernum;

this.custnum = custnum;

}

};

public static class ORDERITEM_DATA {

// struct members

public String ordernum;

public int seqnumber, quantity;

public String itemnum;

// struct constructor

public ORDERITEM_DATA(String ordernum, int seqnumber, int quantity, String itemnum) {

this.ordernum = ordernum;

this.seqnumber = seqnumber;

this.quantity = quantity;

this.itemnum = itemnum;

}

};

static void Add_Transactions() throws IOException, CTException {

ORDER_DATA[] orders = new ORDER_DATA[3];

Calendar orddate0 = Calendar.getInstance();

Calendar promdate0 = Calendar.getInstance();

//Java.util.Calendar.MONTH is Zero Based. ie: January = 0

orddate0.set(2002, 9 - 1, 01);

//Java.util.Calendar.MONTH is Zero Based. ie: January = 0

promdate0.set(2002, 9 - 1, 05);

orders[0] = new ORDER_DATA(orddate0, promdate0, "1", "1001");

Calendar orddate1 = Calendar.getInstance();

Calendar promdate1 = Calendar.getInstance();

//Java.util.Calendar.MONTH is Zero Based. ie: January = 0

orddate1.set(2002, 9-1, 02);

//Java.util.Calendar.MONTH is Zero Based. ie: January = 0

promdate1.set(2002, 9-1, 06);

orders[1] = new ORDER_DATA(orddate1, promdate1, "2", "9999"); // bad customer number

Calendar orddate2 = Calendar.getInstance();

Calendar promdate2 = Calendar.getInstance();

//Java.util.Calendar.MONTH is Zero Based. ie: January = 0

orddate1.set(2002, 9-1, 22);

//Java.util.Calendar.MONTH is Zero Based. ie: January = 0

promdate1.set(2002, 9-1, 26);

orders[2] = new ORDER_DATA(orddate2, promdate2, "3", "1003");

int nOrders = orders.length;

ORDERITEM_DATA[] items = new ORDERITEM_DATA[6];

items[0] = new ORDERITEM_DATA("1", 1, 2, "1");

items[1] = new ORDERITEM_DATA("1", 2, 1, "2");

items[2] = new ORDERITEM_DATA("2", 1, 1, "3");

items[3] = new ORDERITEM_DATA("2", 2, 3, "4");

items[4] = new ORDERITEM_DATA("3", 1, 2, "3");

items[5] = new ORDERITEM_DATA("3", 2, 2, "99"); // bad item number

int nItems = items.length;

int savepoint;

int j = 0;

Delete_Records(recordCustOrdr);

Delete_Records(recordOrdrItem);

System.out.println("\tAdd transaction records...");

// process orders

for (int i = 0; i < nOrders; i++) {

// start a transaction

MySession.Begin();

try {

recordCustOrdr.Clear();

// populate record buffer with order data

recordCustOrdr.SetFieldAsCalendar(0, orders[i].orderdate);

recordCustOrdr.SetFieldAsCalendar(1, orders[i].promdate);

recordCustOrdr.SetFieldAsString(2, orders[i].ordernum);

recordCustOrdr.SetFieldAsString(3, orders[i].custnum);

// add order record

recordCustOrdr.Write();

} catch (CTException E) {

// abort transaction on error

Handle_Exception(E);

}

// set transaction savepoint

savepoint = recordCustOrdr.SetSavePoint();

// process order items

while (items[j].ordernum.equals(orders[i].ordernum)) {

try {

recordOrdrItem.Clear();

// populate record buffer with order item data

recordOrdrItem.SetFieldAsInt(0, items[j].seqnumber);

recordOrdrItem.SetFieldAsInt(1, items[j].quantity);

recordOrdrItem.SetFieldAsString(2, items[j].ordernum);

recordOrdrItem.SetFieldAsString(3, items[j].itemnum);

// add order item record

recordOrdrItem.Write();

// check that item exists in ItemMaster table

recordItemMast.Clear();

recordItemMast.SetFieldAsString(2, items[j].itemnum);

if (!recordItemMast.Find(FIND_MODE.EQUAL)) // if not found, restore back to previous savepoint

{

recordItemMast.RestoreSavePoint(savepoint);

} else // set transaction savepoint

{

savepoint = recordItemMast.SetSavePoint();

}

} catch (CTException E) {

// abort transaction on error

Handle_Exception(E);

}

// bump to next item

j++;

// exit the while loop on last item

if (j >= nItems) {

break;

}

}

// check that customer exists in CustomerMaster table

recordCustMast.Clear();

recordCustMast.SetFieldAsString(0, orders[i].custnum);

// commit or abort the transaction

if (!recordCustMast.Find(FIND_MODE.EQUAL)) {

MySession.Abort();

} else {

MySession.Commit();

}

}

}

//

// Display_CustomerOrders()

//

// This function displays the contents of a table. ctdbFirstRecord() and

// ctdbNextRecord() fetch the record. Then each field is parsed and displayed

//

static void Display_CustomerOrders() throws IOException {

String custnumb;

String ordrnumb;

System.out.println("\tCustomerOrder table...");

try {

// read first record

if (recordCustOrdr.First()) {

do {

ordrnumb = recordCustOrdr.GetFieldAsString(2);

custnumb = recordCustOrdr.GetFieldAsString(3);

// display data

System.out.println("\t" + ordrnumb + "\t" + custnumb);

} // read next record until end of file

while (recordCustOrdr.Next());

}

} catch (CTException E) {

Handle_Exception(E);

}

}

//

// Display_OrderItems()

//

// This function displays the contents of a table. ctdbFirstRecord() and

// ctdbNextRecord() fetch the record. Then each field is parsed and displayed

//

static void Display_OrderItems() throws IOException {

String itemnumb;

String ordrnumb;

System.out.println("\n\tOrderItems Table...");

try {

// read first record

if (recordOrdrItem.First()) {

do {

// get field data from record buffer

ordrnumb = recordOrdrItem.GetFieldAsString(2);

itemnumb = recordOrdrItem.GetFieldAsString(3);

// display data

System.out.println("\t" + ordrnumb + "\t" + itemnumb);

} // read next record until end of file

while (recordOrdrItem.Next());

}

} catch (CTException E) {

Handle_Exception(E);

}

}

TOCIndex