Title: Chapter 15 Creating More Complex Database Applications
1Chapter 15Creating More Complex Database
Applications
2One to One Relationships
15
- There is a one-to-one relationship between
Customer and Boat - A customer always owns exactly one boat and a
boat always belongs to just one customer - Use a database to implement the one-to-one
association using a relational database
3Understanding Tables in CustomerAndBoatDatabase
15
4Understanding Tables in CustomerAndBoatDatabase
15
5Understanding Tables in CustomerAndBoatDatabase
15
6Understanding Tables in CustomerAndBoatDatabase
7Understanding Tables in CustomerAndBoatDatabase
15
8Using SQL to Join Table in Database
15
- SELECT StateRegistration, Manufacturer,
CustomerName, PhoneNoFROM BoatTable,
CustomerTableWHERE CustomerPhoneNo PhoneNo
9Using SQL to Join Table in Database
15
- SELECT StateRegistration, Manufacturer,
CustomerName, PhoneNoFROM BoatTable,
CustomerTableWHERE CustomerPhoneNo PhoneNoAND
StateRegistrationNo MO98765
10Establishing a Common Connection
- CustomerAndBoatDatabaseConnect
- To manage the connection to the database
- - initialize method
- - terminate method
11Initialize method of the CustomerAndBoatDatabaseCo
nnect class
12Terminate method
- public static void terminate( )
- try
- aConnection.close( )
- catch (SQLException e)
- System.out.println(e)
-
13Modifying the Customer PD
- Include a boat reference attribute
- Initialize boat reference to null in constructor
- Include setter and getter methods to set and
retrieve boat reference - Use the common database connection
CustomerAndBoatDatabaseConnect
14Customer with Boat reference attribute and DA
methods (i)
15Customer with Boat reference attribute and DA
methods (ii)
16The BoatDA Class
15
- find, addNew, delete, getAll and update methods
- terminate and initial methods
- Defines a boat reference variable
- a Vector of boat reference variables
17Initialize Terminate methods of the BoatDA
18The BoatDA Class find and getAll Methods
15
- The find method defines a SELECT statement that
retrieves a particular record from BoatTable - throws NotFoundException if record is not found
- The getAll method returns a Vector of boat
references
19- public static Boat find(String key) throws
NotFoundException - aBoat null
- String sqlQuery SELECT StateRegistrationNo,
BoatLength, - Manufacturer,
Year - FROM BoatTable
- WHERE
StateRegistrationNo key - try
- ResultSet rs aStatement.executeQuery(sqlQ
uery) - boolean gotIt rs.next()
- if (gotIt)
- stateRegistrationNo rs.getString(1)
- length rs.getString(2)
- manufacturer rs.getString(3)
- year rs.getInt(4)
- aBoat new Boat(stateRegistrationNo,
length, manufacturer, year) - else throw (new NotFoundException(not
found)) - rs.close()
- return aBoat
20Boat getAll method
21- Public static void addNew(Boat aBoat) throws
DuplicateException - stateRegistrationNo aBoat.getStateRegistration
No() length aBoat.getLength() - manufacturer aBoat.getManufacturer() year
aBoat.getYear() - phoneNo aBoat.getCustomer().getPhoneNo()
- String sqlInsert INSERT INTO BoatTable
(StrateRegistration, BoatLength, Manufacturer,
Year, CustomerPhoneNo) VALUES ( - stateRegistrationNo , length ,
manufacturer , year - , phoneNo )
- try Boat b find(stateRegistrationNo)
- throw (new DuplicateException(Boat
Exists)) - catch (NotFoundException e)
- try int result aStatement.executeUpd
ate(sqlInsert) - catch (SQLException ee)
- System.out.println(ee)
-
22(No Transcript)
23(No Transcript)
24(No Transcript)
25Modifying the Boat Class to Work with BoatDA
15
- The Boat class needs four static methods
- initialize
- find
- getAll
- terminate
- Three instance methods
- addNew
- update
- delete
26(No Transcript)
27(No Transcript)
28(No Transcript)
29Modifying the CustomerDA Class
15
- Change the CustomerDA class to support joining
information from the BoatTable and CustomerTable - Now includes a reference to a Boat object
30CustomerDA Class find and getAll Methods
- Change the SQL statement WHERE clause to specify
both the join condition and the primary key for
the customer of interest
31(No Transcript)
32CustomerDA Class find and getAll Methods
33CustomerDA Class addNew Method
- The is a mandatory one-to-one relationship
between Customer and Boat - Must be a customer record for every boat record
the addNew method must enforce this requirement - After inserting a customer record into
CustomerTable, a record must be inserted into the
BoatTable
34addNew method for CustomerDA class
35CustomerDA Class delete and update Methods
15
- When a customer record is deleted, the
corresponding boat record must be deleted as well - The update method is unchanged
36Delete method of CustomerDA class
37Testing the CustomerAndBoatDatabase Application
15
- public class TesterOne
- static Customer aCustomer
- public static void main(String args )
- Vector customers Boat aBoat
- Connection c CustomerAndBoatDatabaseConnec
t.initialize() - Customer.initialize( c )
- Boat.initialize( c )
- try aCustomer Customer.find(123-4567)
- printDetails( )
- try customers Customer.getAll()
- for(int I 0 I lt customers.size()
i) - aCustomer (Customer)
customers.get(i) - printDetails()
- aCustomer new Customer(Ed, KC,
339-4990) - aBoat new Boat(MO1122233, 25, S-2,
1984) - aBoat.assignBoatToCustomer(aCustomer)
- try aCustomer.addNew()
- try aCustomer Customer.find(339-4990)
- aCustomer.delete()
38One-To-Many Relationship
15
- The relationship between Dock and Slip
- A dock contains many slips
- A slip belongs to one dock
39Understanding the Tables in DockAndSlipDatabase
40Understanding the Tables in DockAndSlipDatabase
41Understanding the Tables in DockAndSlipDatabase
42Common Connection to DockAndSlipDatabase
15
- Same as CustomerAndBoatDatabaseConnect class
except - Different data source is used
43Modifying the Dock and Slip Classes
15
- Dock class must be modified to
- Implement the initialize, terminate, find, and
getAll methods - Dont need to modify the Slip class nor create a
SlipDA class
44Dock class has Slips (i)
45Dock class has Slips (ii)
46Introducing the DockDA Class
15
- Methods used to terminate and initialize are
similar to other DA classes
47DockDA Class find Method
15
48DockDA Class find Method
- public static Dock find(int key) throws
NotFoundException - aDock null
- String sqlQuery SELECT DockTable.DockId,
Location, Electricity, Water, SlipNo, Width,
SlipLength FROM DockTable, SlipTable WHERE
DockTable.DockId key - AND SlipTable.DockId DockTable.DockId
ORDER BY SlipNo - try ResultSet rs aStatement.executeQuery(sql
Query) - boolean dockCreated false
- boolean more rs.next()
- while (more)
- if (dockCreated false)
- dockId rs.getInt(1) location
rs.getString(2) electricityYN rs.getInt(3) - waterYN rs.getInt(4)
- electricity
integerToBoolean(electricityYN) // converts
integers into booleans - water integerToBoolean(waterY
N) - aDock new Dock(dockId,
location, electricity, water) - dockCreated true
- slipNo rs.getInt(5) width
rs.getInt(6) slipLength rs.getDouble(7) - aSlip new Slip(slipNo, width,
slipLength, aDock) //set up the Slips - more rs.next()
- return aDock
49DockDA Class getAll Method
15
50getAll method for DockDA
- Public static Vector getAll( )
- Vector docks new Vector( )
- String sqlQuery SELECT DockTable.DockId,
Location, Electricity, Water, SlipNo, Width,
SlipLength, FROM DockTable, SlipTable Where
SlipTable.DockId DockTable.DockId ORDER BY
DockTable.DockId, SlipNo
51Control-break logic in the getAll method of the
DockDA class
52TesterTwo illustrate Dock has slips association
(i)
53TesterTwo illustrate Dock has slips association
(ii)
54Testing the DockAndSlipDatabase Application
55Implementing an Association Class
- The Lease class has associated AnnualLease and
DailyLease subclasses
56Understanding Tables in CustomerLeaseSlipDatabase
57Understanding Tables in CustomerLeaseSlipDatabase
15
58Modifying the Customer Class
15
- Need to add a lease reference to the CustomerPD
class - Include getter and setter methods for the lease
reference
59Customer with Lease attribute added (i)
60Customer with Lease attribute added (ii)
61Modifying the Lease and AnnualLease Class
15
- Must associate the lease with a customers slip
- Need to add a slip reference and customer
reference - Include getter and setter references to manage
these references
62Lease with Slip Customer references (i)
63Lease with Slip Customer references (ii)
64Modifying the Slip Class
15
- Must associate a slip with its corresponding
lease and customer - Need to add an annual lease reference to the
attribute list and set its value to null - Three DA methods are needed
- Initialize
- Terminate
- Find requires two parameters slip number and
dock ID
65The SlipDA Class
15
- Need a SlipDA class
- Initialize and Terminate methods are identical to
other DA classes - Find method defines a SELECT statement that
returns information from the SlipTable for a
particular slip
66The AnnualLeaseDA Class
- Must find and insert information about annual
leases in the database - Uses standard initialize and terminate methods
67AnnualLeaseDA Class - find
- Query retrieves information from three tables
CustomerTable, LeaseTable, and SlipTable - The setCustomer method establishes a link with
the annual lease instance
68AnnualLease with DA method (i)
69AnnualLease with DA method (ii)
70AnnualLease with DA method (iii)
71AnnualLeaseDA Class - addNew
- Receives a reference to the annual lease instance
that will be added - Lease amount, balance, payment, and start date
are extracted - Before executing SQL statement that inserts into
LeaseTable checks to see if primary key already
exists in the database