This example involves a Department of Motor Vehicles database. The main table is DRIVER; it has a one to one dependent realtionship with the ADDRESS, and CONTACT_INFO tables. The DRIVER table contains foriegn key columns that point at the primary keys of these two tables. The ADDRESS and CONTACT_INFO are dependent on DRIVER and are created, and deleted wlong with it. The Driver.java code generated provides get and set methods for the columns in ADDRESS, and CONTACT_INFO tables. If you call Driver.delete() the corresponding rows on the ADRESS and CONTACT_INFO tables will also be deleted.
Each driver can have zero to many "car"s. The CAR table has a "driver_id" column that is the foreign key that points to the row on the DRIVER table to which this car belongs. New cars can be added to a driver by instantiating a new Car object, calling it's set methods, and then passing it to Driver.addCar(). The Driver.listCars() method returns an iteration of the cars that belong to this driver. If you call Driver.delete() the corresponding rows on the CARS table will also be deleted.
The ddl for creating the table and sequences is in table-definition.sql.
Driver d = new Driver();
d.setFirstName("Peter");
d.setMiddleName("Maurice");
d.setLastName("Parker");
d.setGender("M");
d.setLicensePoints(2.0f);
TimeZone timezone = TimeZone.getDefault();
GregorianCalendar gc = new GregorianCalendar(timezone);
gc.add(Calendar.YEAR, -20);
d.setDateOfBirth(gc);
d.setAddressLine1("10 Main st");
d.setAddressLine2("");
d.setCity("Brooklyn");
d.setState("NY");
d.setZip("12233");
d.setHomePhone("718-123-45678");
d.setWorkPhone("212-444-5555");
d.setEmailAddress("pp@ff.org");
try {
key = d.store();
}
catch (Exception exp) {
System.err.println("The store of Driver failed: " + exp.getMessage());
exp.printStackTrace();
}
// Update this driver
d = new Driver(key);
d.setMiddleName("paul");
d.setAddressLine1("1 Fifth Ave");
d.setWorkPhone("516-444-5555");
float points = d.getLicensePoints();
points += 1.5f; // got a ticket worth 1.5 points
d.setLicensePoints(points);
try {
d.store();
}
catch (Exception exp) {
System.err.println("The store of Driver failed: " + exp.getMessage());
exp.printStackTrace();
}
// add a car owned by this driver
Car c = new Car();
c.setMake("Ford");
c.setModel("Escort");
c.setYear("1999");
c.setMileage(10000);
try {
d.addCar(c);
}
catch (Exception exp) {
System.err.println("The add of Car failed: " + exp.getMessage());
exp.printStackTrace();
}
// Display a driver
d = new Driver(key);
System.out.println("\n\n\n Displaying a Driver:");
System.out.println(" firstName = " + d.getFirstName() );
System.out.println(" middleName = " + d.getMiddleName() );
System.out.println(" lastName = " + d.getLastName() );
System.out.println(" gender = " + d.getGender() );
System.out.println(" licensePoints = " + d.getLicensePoints() );
System.out.println(" dateOfBirth = " + (d.getDateOfBirth().getTime().toString()) );
System.out.println(" address_line1 = " + d.getAddressLine1() );
System.out.println(" address_line2 = " + d.getAddressLine2() );
System.out.println(" city = " + d.getCity() );
System.out.println(" state = " + d.getState() );
System.out.println(" zip = " + d.getZip() );
System.out.println(" home_phone = " + d.getHomePhone() );
System.out.println(" work_phone = " + d.getWorkPhone() );
System.out.println(" email_address = " + d.getEmailAddress() );
System.out.println(" Cars owned by this Driver:");
iterator = d.listCars();
while (iterator.hasNext() ) {
Car car = (Car)iterator.next();
System.out.println(" make = " + car.getMake() );
System.out.println(" model = " + car.getModel() );
System.out.println(" year = " + car.getYear() );
System.out.println(" mileage = " + car.getMileage() );
}
// Remove any Chevys that this driver owns
key = 1; // Need to know the primary key of the driver
d = new Driver(key);
System.out.println("Removing any chevys owned by this driver:");
iterator = d.listCars();
while (iterator.hasNext() ) {
Car car = (Car)iterator.next();
if ( car.getMake().equalsIgnoreCase("chevy") ) {
try {
d.removeCar(car);
}
catch (Exception exp) {
System.err.println("The add of Car failed: " + exp.getMessage());
exp.printStackTrace();
}
}
}
// Delete a driver, and all dependent address, contact info, and cars
key = 1; // Need to know the primary key of the driver
d = new Driver(key);
try {
d.delete();
}
catch (Exception exp) {
System.err.println("The delete of Driver failed: " + exp.getMessage());
exp.printStackTrace();
}