Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes.
Transcript of Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes.
Chapter 14 - Designing Data Access Classes 1
Chapter 14
Designing Data Access Classes
Chapter 14 - Designing Data Access Classes 2
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 3
Making Objects Persistent
Chapter 14 - Designing Data Access Classes 4
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 5
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 6
Data Access Methods
Chapter 14 - Designing Data Access Classes 7
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
14
Chapter 14 - Designing Data Access Classes 8
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
14
Chapter 14 - Designing Data Access Classes 9
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 10
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 11
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 12
Understanding Java I/O Java views data input and output as a flow
of bytes Two different data streams:
Byte stream Character stream
14
Chapter 14 - Designing Data Access Classes 13
Understanding Java I/O14
Chapter 14 - Designing Data Access Classes 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
14
Chapter 14 - Designing Data Access Classes 15
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 packageimport java.io.*;
14
Chapter 14 - Designing Data Access Classes 16
Initialize Method Reads the attribute values for all of the
customers from the sequential file Uses BufferReader class to read the file
sequentially
14
Chapter 14 - Designing Data Access Classes 17
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
14
Chapter 14 - Designing Data Access Classes 18
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 19
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 20
Update Method Contains no code – since the file is written
when done
14
Chapter 14 - Designing Data Access Classes 21
Delete Method Remove a customer from the system Removes the reference from the Vector If the customer is not found a
NotFoundException is thrown
14
Chapter 14 - Designing Data Access Classes 22
getAll Method The Vector already contains all of the
customers Simply returns this Vector
Chapter 14 - Designing Data Access Classes 23
Testing CustomerDA Class1. Create two customer instances2. Invoke initialize3. Invoke addNew to add two customers4. Retrieve reference to first customer5. Invoke getAll6. Invoke delete7. Change first customer’s address and verify8. Invoke the terminate method
14
Chapter 14 - Designing Data Access Classes 24
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 25
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 26
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
14
Chapter 14 - Designing Data Access Classes 27
Designing a Relational Database14
Chapter 14 - Designing Data Access Classes 28
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
14
Chapter 14 - Designing Data Access Classes 29
Understanding SQL 14