Chapter 14 Designing Data Access Classes - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Chapter 14 Designing Data Access Classes

Description:

It's a non-static method because it's invoked for the new customer being added ... Database is one or more files that help make queries ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 30
Provided by: johnf191
Category:

less

Transcript and Presenter's Notes

Title: Chapter 14 Designing Data Access Classes


1
Chapter 14Designing 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 objects attributes
    individually
  • Object storage store the entire object
  • Advantage of object storage is that you dont
    need to recreate the object when you retrieve it

3
Making Objects Persistent
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

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

6
Data Access Methods
7
Communicating with DA Class
14
  • 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

8
Finding a Customer
14
  • The purpose of the PD find method is to invoke
    the DA find method
  • Its a static method because its not tied to a
    specific customer instance
  • Throws a NotFoundException if the customer is not
    found

9
Adding a Customer
  • The purpose of the PD addNew method is to invoke
    the DA addNew method
  • Its a non-static method because its invoked for
    the new customer being added
  • Throws a DuplicateException if the customer
    already exists

10
Changing a Customer
  • The purpose of the PD update method is to invoke
    the DA update method
  • Its a non-static method because its invoked for
    the customer being updated
  • Throws a NotFoundException if the customer is not
    found

11
Deleting a Customer
  • The purpose of the PD delete method is to invoke
    the DA delete method
  • Its a non-static method because its invoked for
    the customer being deleted
  • Throws a NotFoundException if the customer is not
    found

12
Understanding Java I/O
14
  • Java views data input and output as a flow of
    bytes
  • Two different data streams
  • Byte stream
  • Character stream

13
Understanding Java I/O
14
14
Understanding Java I/O
14
  • 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

15
Implementing Persistence with a Sequential File
14
  • 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.

16
Initialize Method
14
  • Reads the attribute values for all of the
    customers from the sequential file
  • Uses BufferReader class to read the file
    sequentially

17
Terminate Method
14
  • 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

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

19
AddNew Method
  • Simply adds the new reference to the Vector
  • If a duplicate is found throws a
    DuplicateException error

20
Update Method
14
  • Contains no code since the file is written when
    done

21
Delete Method
14
  • Remove a customer from the system
  • Removes the reference from the Vector
  • If the customer is not found a NotFoundException
    is thrown

22
getAll Method
  • The Vector already contains all of the customers
  • Simply returns this Vector

23
Testing CustomerDA Class
14
  • Create two customer instances
  • Invoke initialize
  • Invoke addNew to add two customers
  • Retrieve reference to first customer
  • Invoke getAll
  • Invoke delete
  • Change first customers address and verify
  • Invoke the terminate method

24
Implementing Persistence with Random Access
  • Change the file specification to Customer.ran
  • Use the RandomAccessFile class
  • Use the writeBytes method

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

26
Designing a Relational Database
14
  • 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)

27
Designing a Relational Database
14
28
Understanding SQL
14
  • 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

29
Understanding SQL
14
Write a Comment
User Comments (0)
About PowerShow.com