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()

//

// This function performs record adds and updates using locking

//

static void Manage() throws IOException {

System.out.println("MANAGE");

// populate the table with data

Add_CustomerMaster_Records();

// display contents of table

Display_Records();

// update a record under locking control

Update_CustomerMaster_Record();

// display again after update and effects of lock

Display_Records();

}

//

// Add_CustomerMaster_Records()

//

// This function adds records to table CustomerMaster from an

// array of Strings

//

public static class DATA_RECORD {

// struct members

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

// struct constructor

public DATA_RECORD(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 {

DATA_RECORD[] data = new DATA_RECORD[4];

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

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

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

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

int nRecords = data.length;

Delete_Records(MyRecord);

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

try {

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

MyRecord.Clear();

// populate record buffer with data

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

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

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

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

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

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

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

// add record

MyRecord.Write();

}

} 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 {

// enable session-wide lock flag

MySession.Lock(LOCK_MODE.WRITE_BLOCK);

// read first record

found = record.First();

while (found) // while records are found

{

// delete record

record.Delete();

// read next record

found = record.Next();

}

// reset session-wide locks

MySession.Unlock();

} catch (CTException E) {

Handle_Exception(E);

}

}

//

// Display_Records()

//

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

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

//

static void Display_Records() throws IOException {

boolean found;

String custnumb;

String custname;

System.out.print("\tDisplay records...");

try {

// read first record

found = MyRecord.First();

while (found) {

custnumb = MyRecord.GetFieldAsString(0);

custname = MyRecord.GetFieldAsString(4);

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

// read next record

found = MyRecord.Next();

}

} catch (CTException E) {

Handle_Exception(E);

}

}

//

// Update_CustomerMaster_Records()

//

// Update one record under locking control to demonstrate the effects

// of locking

//

static void Update_CustomerMaster_Record() throws IOException {

System.out.println("\tUpdate Record...");

try {

// enable session-wide lock flag

MySession.Lock(LOCK_MODE.WRITE_BLOCK);

MyRecord.Clear();

MyRecord.SetFieldAsString(0, "1003");

// find record by customer number

if (MyRecord.Find(FIND_MODE.EQUAL)) {

MyRecord.SetFieldAsString(4, "KEYON DOOLING");

// rewrite record

MyRecord.Write();

System.out.println("\tPress <ENTER> key to unlock");

System.in.read();

}

// reset session-wide locks

MySession.Unlock();

} catch (CTException E) {

Handle_Exception(E);

}

}

TOCIndex