Presentation is loading. Please wait.

Presentation is loading. Please wait.

ATS Application Programming: Java Programming

Similar presentations


Presentation on theme: "ATS Application Programming: Java Programming"— Presentation transcript:

1 ATS Application Programming: Java Programming
9.3 Data Access Objects 9.3 Data Access Objects ATS Application Programming: Java Programming © Accenture 2005 All Rights Reserved Course Code #Z16325

2 ATS Application Programming: Java Programming
Objectives 9.3 Data Access Objects Define Data Access Object (DAO) Introduce the DAO classes in the HRS Application Introduce the methods of DAO Learn the steps in using DAO Use the transaction handling mechanism of DAO © Accenture 2005 All Rights Reserved Course Code #Z16325

3 ATS Application Programming: Java Programming
Data Access Object 9.3 Data Access Objects The Data Access Object (also known simply as DAO) implements the access mechanism required to work with the data source. Regardless of what type of data source is used, the DAO always provides a uniform API to its clients. In this case, the data source used is a relational database management system (RDBMS) as the persistent store. The business component that needs data access uses the simpler interface exposed by the DAO for its clients. The DAO completely hides the data source implementation details from its clients. Because the interface exposed by the DAO to clients does not change when the underlying data source implementation changes, it allows you to change a DAO implementation without changing the DAO client's implementation. Essentially, the DAO acts as an adapter between the component and the data source. © Accenture 2005 All Rights Reserved Course Code #Z16325

4 ATS Application Programming: Java Programming
Data Access Object 9.3 Data Access Objects There are 5 data access object classes that represent each table’s entities in the HRS application: ProjectDAO SkillDAO SkillCategoryDAO EmployeeDAO EmpAccentureDetailsDAO Each class implements methods from DataAccessObjectInterface Input parameters of these methods are mostly in each corresponding bean class format that represents its data model (see com.jds.apps.beans package for the list of bean classes) © Accenture 2005 All Rights Reserved Course Code #Z16325

5 Data Access Object Methods
ATS Application Programming: Java Programming Data Access Object Methods 9.3 Data Access Objects create method signature: public void create(Connection conn, Object object) throws DAOException update method signature: public boolean update(Connection conn, Object objSet, Object objWhere) remove method signature: public boolean remove(Connection conn, Object object) findByPK method signature: public Object findByPK(Object object) The codes can be found in com.jds.architecture.service.dao (EmployeeDao.java) Create – Inserts a new entry in the table. Parameter: Connection conn - database connection Object object – For tables with one primary key, string object containing the primary key. For composite keys, corresponding bean object containing the keys Return: None Exception: DAOException, thrown when input object is not the correct type DAOException, thrown when improper sql execution DAOException, when other unspecific exception is thrown Update - Updates a table entry Object objSet – Corresponding bean object containing the values to be set. Object objectWhere – Corresponding bean object containing the values for criteria. DAOException, thrown when an error occurred while creating the sql statement Remove – Removes an entry from the table FindbyPK – Looks for the specific table entry. Object object – Corresponding bean object containing the keys Return: Bean class containing details of the table entry found. Create – Inserts a new entry in the table. Parameter: Connection conn - database connection Object object – For tables with one primary key, a string object containing the primary key. For composite keys, corresponding bean object containing the keys Return: None Exception: DAOException, thrown when input object is not the correct type DAOException, thrown when improper sql execution DAOException, when other unspecific exception is thrown Update - Updates a table entry Object objSet – Corresponding bean object containing the values to be set. Object objectWhere – Corresponding bean object containing the values for criteria. DAOException, thrown when an error occurred while creating the sql statement Remove – Removes an entry from the table Object object – For tables with one primary key, string object containing the primary key. For composite keys, corresponding bean object containing the keys FindbyPK – Looks for the specific table entry. Object object – Corresponding bean object containing the keys Return: Bean class containing details of the table entry found. © Accenture 2005 All Rights Reserved Course Code #Z16325

6 Data Access Object Methods
ATS Application Programming: Java Programming Data Access Object Methods 9.3 Data Access Objects find method signature: public RowSet find(Object object) throws DAOException findByAll method signature: public RowSet findByAll() throws DAOException Find - Looks for matched table entries Parameter: Object object – Corresponding bean object containing the keys Return: Rowset containing matched entries. Exception: DAOException, thrown when an error occurred while creating the sql statement DAOException, thrown when input object is not the correct type DAOException, thrown when improper sql execution DAOException, when other unspecific exception is thrown FindByAll - Retrieves all table entries. Parameter: None Return: Rowset containing matched entries © Accenture 2005 All Rights Reserved Course Code #Z16325

7 Steps in Using Data Access Object
ATS Application Programming: Java Programming Steps in Using Data Access Object 9.3 Data Access Objects Import necessary packages (bean classes, DAO and db access) Get an instance of the object Get database connection when using data manipulation methods (create, remove and update) Call the method Additional steps for data manipulation methods: Commit connection Rollback transaction if exception occurred Close the connection © Accenture 2005 All Rights Reserved Course Code #Z16325

8 Data Access Object Transaction Handling
ATS Application Programming: Java Programming Data Access Object Transaction Handling 9.3 Data Access Objects Transactional operations are handled by passing the database connection that will execute and commit the database update An example of this operation is the, “create employee” There are two table updates that are involved in creating an employee: 1. Insert new entry for employee profile and 2. Insert new entry for the corresponding Accenture details All of these steps should have successful execution to create a new employee Faculty Notes: In implementing a transaction, it is important to know when should an operation be committed and when it will be rolled back. In general practice, rollback operations are placed in exception catch. © Accenture 2005 All Rights Reserved Course Code #Z16325

9 Data Access Object Sample Code General Steps
ATS Application Programming: Java Programming Data Access Object Sample Code General Steps 9.3 Data Access Objects //1.Import necessary packages. import com.jds.apps.Constants; import com.jds.apps.beans.*; import com.jds.architecture.service.dao.*; import com.jds.architecture.service.dbaccess.*; //2.Get an instance of the object. DataAccessObjectInterface empDao = (EmployeeDAO)DAOFactory.getFactory() .getDAOInstance(DAOConstants.DAO_EMP); All the lines above and on the next slide can be seen in com.jds.businesscomponent.hr (EmployeeBC.java) Emphasize to participants that according to Coding Standards, importation of necessary packages should be done in a complete manner instead of putting a wildcard in the end of the import statement (e.g. com.jds.apps.beans.*). For the complete list of the imported packages, they can just browse on the said java file (EmployeeBC.java). //3.Get database connection when using data manipulation methods (create , remove and update) DBAccess dbAccess = DBAccess.getDBAccess(); Connection conn = dbAccess.getConnection(); © Accenture 2005 All Rights Reserved Course Code #Z16325

10 Data Access Object Sample Code General Steps
ATS Application Programming: Java Programming Data Access Object Sample Code General Steps 9.3 Data Access Objects //4.Call the method. //non data manipulation method id = EmployeeIdGenerator.getInstance().getNextId(); info.setEmpNo(String.valueOf(id)); empDao.create(conn, info); RowSet set = empDao.find(info); AccentureDetails details = info.getAccentureDetails(); details.setEmployeeNo(info.getEmpNo()); empAccDao.create(conn, details); OR //data manipulation method try{ empDao.create(conn,info); //5.Commit connection. conn.commit(); } catch (DAOException e) { © Accenture 2005 All Rights Reserved Course Code #Z16325

11 Data Access Object Sample Code General Steps
ATS Application Programming: Java Programming Data Access Object Sample Code General Steps 9.3 Data Access Objects //6.Rollback transaction if exception occurred. dbAccess.rollbackConnection(conn); if (e.isLogical()) throw new HRSLogicalException (e.getMessageKey()); else throw new HRSSystemException (e.getMessageKey(),e.getCause()); } catch (Exception e) { throw new HRSSystemException ("business.component.exception",e.getCause()); } finally { //7.Close the connection. dbAccess.closeConnection(conn); } catch (DBAccessException e) { throw new HRSSystemException (e.getMessageKey(),e.getCause()); } © Accenture 2005 All Rights Reserved Course Code #Z16325

12 Data Access Object Sample Code Transaction Handling
ATS Application Programming: Java Programming Data Access Object Sample Code Transaction Handling 9.3 Data Access Objects try{ try { conn = dbAccess.getConnection(); empDao.create(conn, info); RowSet set = empDao.find(info); AccentureDetails details = info.getAccentureDetails(); if (set.next()) { details.setEmployeeNo(set.getString("empno")); } empAccDao.create(conn, details); //commit employee and Accenture detail create entry conn.commit(); } catch (DAOException e) { © Accenture 2005 All Rights Reserved Course Code #Z16325

13 Data Access Object Sample Code Transaction Handling
ATS Application Programming: Java Programming Data Access Object Sample Code Transaction Handling 9.3 Data Access Objects //rollback all operations on exception dbAccess.rollbackConnection(conn); //TODO checking for rollback if (e.isLogical()) throw new HRSLogicalException (e.getMessageKey()); else throw new HRSSystemException (e.getMessageKey(),e.getCause()); } catch (Exception e) { throw new HRSSystemException ("business.component.exception",e.getCause()); } finally { dbAccess.closeConnection(conn); } } catch (DBAccessException e) { throw new HRSSystemException (e.getMessageKey(),e.getCause()); © Accenture 2005 All Rights Reserved Course Code #Z16325

14 ATS Application Programming: Java Programming
Key Points 9.3 Data Access Objects DAO implements the access mechanism required to work with the data source, regardless of what type of data source is used Each table entity in the HRS Application is represented by a DAO class Methods in DAO used in HRS Application are as follows: create(), update(), remove(), findByPK(), find(), findByAll() DAO is also used to implement Transaction Management © Accenture 2005 All Rights Reserved Course Code #Z16325

15 Questions and Comments
ATS Application Programming: Java Programming Questions and Comments 9.3 Data Access Objects ??? © Accenture 2005 All Rights Reserved Course Code #Z16325


Download ppt "ATS Application Programming: Java Programming"

Similar presentations


Ads by Google