Enterprise Java Development@TOPIC@

Data Access Objects (DAOs)

An Introduction to DAOs, DB Schema, and SQL

Revision: v2013-09-21

Built on: 2014-03-06 23:57 EST

Abstract

This presentation provides and introduction to the Data Access Object (DAO) pattern to encapsulate access to data, database schema used to design data organization in RDBMS systems, and SQL to access the data.


Purpose
1. Goals
2. Objectives
1. Data Access Object (DAO) Pattern
1.1. Context
1.2. Problem
1.3. Forces
1.4. Solution
1.5. DAO Implementation Structure
1.6. Consequences
2. DAO Interface
2.1. DAO Interface
2.2. DAO Exceptions
2.3. DAO Implementation
2.4. DAO Test
3. Data Transfer Object (DTO) Pattern
3.1. Context
3.2. Problem
3.3. Forces
3.4. Solution
3.5. Consequences
4. RDBMS Schema
4.1. RDBMS
4.1.1. Background
4.1.2. Tables/Columns
4.1.3. Constraints
4.1.4. Relationships
4.1.5. Indexes
4.2. Data Definition Language (DDL)
4.2.1. Create Table
4.2.2. Drop Table
4.2.3. Create Foreign Key Constraint
4.2.4. Drop Foreign Key Constraint
4.2.5. Create Index
4.2.6. Drop Index
4.3. DDL Files in Maven Module
5. SQL Basics
5.1. Create/INSERT
5.2. Read/SELECT
5.3. Update/UPDATE
5.4. Delete/DELETE
6. Working with Native SQL
6.1. Java Database Connectivity (JDBC)
6.2. Java Persistence API (JPA) Native SQL

  • DBMS based on a relational model

  • Introduced by E. F. Codd in 1970s

  • Some challenges by other forms but still remains a standard for corporate data stores

public class JDBCBookDAOImpl implements BookDAO {

    private Connection connection;
    
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
    @Override
    public Book create(Book book) throws PersistenceException {
        PreparedStatement statement=null;
        ResultSet rs = null;
        try {
            statement=connection.prepareStatement(
                "insert into JPADAO_BOOK (ID, DESCRIPTION, PAGES, TITLE) " +
                "values (null, ?, ?, ?)");
            statement.setString(1, book.getDescription());
            statement.setInt(2, book.getPages());
            statement.setString(3, book.getTitle());
            statement.execute();
            statement.close();
            
            Field id = Book.class.getDeclaredField("id");
            id.setAccessible(true);
            statement = connection.prepareStatement("call identity()");
            rs = statement.executeQuery();
            if (rs.next()) {
                id.set(book, rs.getLong(1));
            }
            
            return book;
        } catch (SQLException ex) { 
            throw new PersistenceException("SQL error creating book", ex);
        } catch (NoSuchFieldException ex) {
            throw new RuntimeException("Error locating id field", ex);
        } catch (SecurityException ex) {
            throw new RuntimeException("Security error setting id", ex);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException("Error setting id", ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException("Access error setting id", ex);
        } finally {
            try { rs.close(); } catch (Exception ex){}
            try { statement.close(); } catch (Exception ex){}
        }
    }
    ...
}