Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes.

Similar presentations


Presentation on theme: "Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes."— Presentation transcript:

1 Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes

2 Chapter 14 - Designing Data Access Classes2 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

3 Chapter 14 - Designing Data Access Classes3 Making Objects Persistent

4 Chapter 14 - Designing Data Access Classes4 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 Chapter 14 - Designing Data Access Classes5 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 Chapter 14 - Designing Data Access Classes6 Data Access Methods

7 Chapter 14 - Designing Data Access Classes7 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

8 Chapter 14 - Designing Data Access Classes8 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

9 Chapter 14 - Designing Data Access Classes9 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

10 Chapter 14 - Designing Data Access Classes10 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

11 Chapter 14 - Designing Data Access Classes11 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

12 Chapter 14 - Designing Data Access Classes12 Understanding Java I/O  Java views data input and output as a flow of bytes  Two different data streams:  Byte stream  Character stream 14

13 Chapter 14 - Designing Data Access Classes13 Understanding Java I/O 14

14 Chapter 14 - Designing Data Access Classes14 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

15 Chapter 14 - Designing Data Access Classes15 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.*; 14

16 Chapter 14 - Designing Data Access Classes16 Initialize Method  Reads the attribute values for all of the customers from the sequential file  Uses BufferReader class to read the file sequentially 14

17 Chapter 14 - Designing Data Access Classes17 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

18 Chapter 14 - Designing Data Access Classes18 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 Chapter 14 - Designing Data Access Classes19 AddNew Method  Simply adds the new reference to the Vector  If a duplicate is found throws a DuplicateException error

20 Chapter 14 - Designing Data Access Classes20 Update Method  Contains no code – since the file is written when done 14

21 Chapter 14 - Designing Data Access Classes21 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

22 Chapter 14 - Designing Data Access Classes22 getAll Method  The Vector already contains all of the customers  Simply returns this Vector

23 Chapter 14 - Designing Data Access Classes23 Testing CustomerDA Class 1.Create two customer instances 2.Invoke initialize 3.Invoke addNew to add two customers 4.Retrieve reference to first customer 5.Invoke getAll 6.Invoke delete 7.Change first customer’s address and verify 8.Invoke the terminate method 14

24 Chapter 14 - Designing Data Access Classes24 Implementing Persistence with Random Access  Change the file specification to Customer.ran  Use the RandomAccessFile class  Use the writeBytes method

25 Chapter 14 - Designing Data Access Classes25 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 Chapter 14 - Designing Data Access Classes26 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

27 Chapter 14 - Designing Data Access Classes27 Designing a Relational Database 14

28 Chapter 14 - Designing Data Access Classes28 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

29 Chapter 14 - Designing Data Access Classes29 Understanding SQL 14


Download ppt "Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes."

Similar presentations


Ads by Google