Transcript Document

Chapter 14
Designing Data Access
Classes
Chapter 14 - Designing Data Access Classes
1
Making Objects Persistent
 Use object persistence to store instances or
attribute values for later retrieval
 Two approaches to achieving persistence:
 Attribute storage – store the object’s attributes
individually
 Object storage – store the entire object
 Advantage of object storage is that you don’t need
to recreate the object when you retrieve it
Chapter 14 - Designing Data Access Classes
2
Making Objects Persistent
Chapter 14 - Designing Data Access Classes
3
Designing a Data Access Class
 The purpose of the Data Access (DA) class is to
provide methods to store and retrieve data and
make instances of a Problem Domain (PD) class
persistence
 Three tier OO design
 GUI class
 PD class for business entities
 DA class for data storage and retrieval
 DA class isolates data storage and retrieval
 DA class supports three-tier architecture
Chapter 14 - Designing Data Access Classes
4
Data Access Methods
 Since you will not have instances of the DA class –
all methods will be static
 Four basic methods are provided




Retrieve – find method
Store – addNew method
Change – update method
Remove – delete method
Chapter 14 - Designing Data Access Classes
5
Data Access Methods
Chapter 14 - Designing Data Access Classes
6
14
Communicating with DA Class
 The Customer class invokes methods in
CustomerDA class
 Isolates the DA class from everything but PD class
 DA class methods are not tied to data storage
method
Chapter 14 - Designing Data Access Classes
7
14
Finding a Customer
 The purpose of the PD find method is to invoke
the DA find method
 It’s a static method because it’s not tied to a
specific customer instance
 Throws a NotFoundException if the customer is
not found
Chapter 14 - Designing Data Access Classes
8
Adding a Customer
 The purpose of the PD addNew method is to
invoke the DA addNew method
 It’s a non-static method because it’s invoked for
the new customer being added
 Throws a DuplicateException if the customer
already exists
Chapter 14 - Designing Data Access Classes
9
Changing a Customer
 The purpose of the PD update method is to invoke
the DA update method
 It’s a non-static method because it’s invoked for
the customer being updated
 Throws a NotFoundException if the customer is
not found
Chapter 14 - Designing Data Access Classes
10
Deleting a Customer
 The purpose of the PD delete method is to invoke
the DA delete method
 It’s a non-static method because it’s invoked for
the customer being deleted
 Throws a NotFoundException if the customer is
not found
Chapter 14 - Designing Data Access Classes
11
14
Understanding Java I/O
 Java views data input and output as a flow of
bytes
 Two different data streams:
 Byte stream
 Character stream
Chapter 14 - Designing Data Access Classes
12
Understanding Java I/O
Chapter 14 - Designing Data Access Classes
14
13
14
Understanding Java I/O
 Record is a collection of related variables
 File is a collection of related records
 Sequential files contains records that are stored and
processed in sequential order
 Random access files are organized so you can access a
record by specifying its record number
 Database is one or more files that help make queries
 Relational database organized in tables and rows
Chapter 14 - Designing Data Access Classes
14
14
Implementing Persistence with a Sequential File
 Reads the customer records, creates a customer
instance for each record, and places instances in
a Vector
 Uses the java.io package
import java.io.*;
Chapter 14 - Designing Data Access Classes
15
14
Initialize Method
 Reads the attribute values for all of the customers
from the sequential file
 Uses BufferReader class to read the file
sequentially
Chapter 14 - Designing Data Access Classes
16
14
Terminate Method
 Responsible for creating a file that contains
attribute values for all the customer instances in
the Vector
 Attributes written to the file using the println
method
Chapter 14 - Designing Data Access Classes
17
Find Method
 The find method simply iterates the customers
Vector, seeking a customer instance with a phone
number that matches the value received by the
method
 If the customer is not found a NotFoundException
is thrown
Chapter 14 - Designing Data Access Classes
18
AddNew Method
 Simply adds the new reference to the Vector
 If a duplicate is found throws a DuplicateException
error
Chapter 14 - Designing Data Access Classes
19
14
Update Method
 Contains no code – since the file is written when
done
Chapter 14 - Designing Data Access Classes
20
14
Delete Method
 Remove a customer from the system
 Removes the reference from the Vector
 If the customer is not found a NotFoundException
is thrown
Chapter 14 - Designing Data Access Classes
21
getAll Method
 The Vector already contains all of the customers
 Simply returns this Vector
Chapter 14 - Designing Data Access Classes
22
14
Testing CustomerDA Class
1.
2.
3.
4.
5.
6.
7.
8.
Create two customer instances
Invoke initialize
Invoke addNew to add two customers
Retrieve reference to first customer
Invoke getAll
Invoke delete
Change first customer’s address and verify
Invoke the terminate method
Chapter 14 - Designing Data Access Classes
23
Implementing Persistence with Random Access
 Change the file specification to Customer.ran
 Use the RandomAccessFile class
 Use the writeBytes method
Chapter 14 - Designing Data Access Classes
24
Implementing Persistence with Object Serialization
 Store the entire Customer instances
 Change the file specification to Customer.obj
 Use the ObjectInputStream and the readObject
method
 Use the ObjectOutputStream and the writeObject
method
Chapter 14 - Designing Data Access Classes
25
14
Designing a Relational Database
Provide tools to organize data into tables
Each column represents a field
Each row represents a record
Each customer is identified by their phone number
– primary key
 Protocols required to access Microsoft Access




 Java Database Connectivity (JDBC)
 Open Database Connectivity (ODBC)
 See pp. 510 for setup of DSN
Chapter 14 - Designing Data Access Classes
26
14
Designing a Relational Database
Chapter 14 - Designing Data Access Classes
27
14
Understanding SQL
 Structured Query Language (SQL) used to access
relational databases
 SQL SELECT is used to retrieve a specific customer
record
 SQL INSERT is used to add a new customer record
 SQL UPDATE is used to change a customer record
 SQLQ DELETE is used to delete a customer record
 Java.sql package contains the database access method
Chapter 14 - Designing Data Access Classes
28
Understanding SQL
Chapter 14 - Designing Data Access Classes
14
29