ATS Application Programming: Java Programming

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Copyright  Oracle Corporation, All rights reserved. 2 Java and Databases: An Overview.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Distributed Application Development B. Ramamurthy.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Agenda Journalling More Embedded SQL. Journalling.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
BEST PRACTICES - Java By Configuration Use global-forwards/results Helps to avoid duplicate jsp files and redundancy forward mapping.
Database Programming in Java Corresponds with Chapter 32, 33.
EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Message-Driven Beans and EJB Security Lesson 4B / Slide 1 of 37 J2EE Server Components Objectives In this lesson, you will learn about: Identify features.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
SQLite Supported by BlackBerry OS 5.0 Using SQLite.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes.
3-Tier Client/Server Internet Example. TIER 1 - User interface and navigation Labeled Tier 1 in the following graphic, this layer comprises the entire.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
آرمان حسين‌زاده آذر  Access to data varies depending on the source of the data.  Access to persistent storage, such as to a database, varies greatly.
Presentation & Business Tier Design Patterns Pearce.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Data Base Connectivity From JAVA Creating a Java program that access mySQL is not difficult. Your Java program must can a number of functions to facilitate.
Data Structures and Java CS /14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Chapter 15: Reliability and Security in Database Servers Neyha Amar CS 157B May 6, 2008.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
JDBC.
ASP.NET Programming with C# and SQL Server First Edition
Entity Bean Chuyên đề Lập trình Java & J2EE Chương 15
Lecture 14 Throwing Custom Exceptions
JDBC – Java Database Connectivity
Relational Database Design
Web Technologies IT230 Dr Mohamed Habib.
Chapter 6: The Stack Abstract Data Type
How to connect natively?
Java Programming Language
CIS 764 Database Systems Engineering
SQL – Application Persistence Design Patterns
EE422C Software Implementation II
Introduction to Server-Side Web Development using JSP and Databases
Java Programming Language
Teaching slides Chapter 8.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
PT2520 Unit 9: Database Security II
Packages and Interfaces
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Objectives In this lesson, you will learn to:
Java Database Connectivity
Entity Beans B.Ramamurthy 2/17/2019 BR.
Understanding and Designing with EJB
CMSC 202 Exceptions 2nd Lecture.
Enterprise Java Beans.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Objectives In this lesson, you will learn about:
SQL – Application Persistence Design Patterns
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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