DAO Code

Overview of DAO Code types

There are three types of DAO code: Table DAO code, view DAO code, and Custom DAO code.

Generate DAOs: Download FireStorm/DAO

Table DAO Code

Table DAO code is the most widely used Data Access Object. Each Table DAO represents a single table and contains methods to insert, update, and delete single rows using the following signatures (using an example Customer table):

public void insert(Customer customer) throws CustomerDaoException

public void update(CustomerPk pk, Customer customer) throws CustomerDaoException

public void delete(CustomerPk pk) throws CustomerDaoException

In addition to this, there are also numerous 'finder' methods generated. FireStorm/DAO creates default finders when you first import a schema but these are fully customizable. Some examples of finder methods:

public Customer findByPrimaryKey(CustomerPk pk) throws CustomerDaoException;

public Customer[] findWhereLastNameEquals(String lastName) throws CustomerDaoException;

public Customer[] findByCountry(int countryId) throws CustomerDaoException;

View DAO Code

The View DAO is generated for each view in the database. This DAO code offers the same finder methods as the Table DAO code but obviously does not provide the insert, update, and delete operations because views are read-only.

Custom DAO Code

Custom DAO code is used when you have a complex SQL query that goes beyond the simple CRUD (create, update, delete) operations on a single table. Examples would be SQL queries that perform a join between several tables, queries that performs aggregration using the GROUP BY operator, and bulk update or delete queries.

Example queries that the Custom DAO code feature in FireStorm/dAO can support:

SELECT a.*, b.* FROM a, b WHERE a.id = b.id AND b.install_date between ? and ?

DELETE customer WHERE status = ? AND create_date = ?

SELECT product, count(*) FROM product WHERE download_date > ? GROUP BY product

Sample code for calling the generated DAO classes

// obtain DAO instance for the Customer table
CustomerDao dao = CustomerDaoFactory.create();

// create a new customer
Customer newCust = new Customer();
newCust.setEmail( "joe@bloggs.com" );
newCust.setFirstName( "joe" );
newCust.setLastName( "bloggs" );

// insert into the database
dao.insert( newCust );

// perform a query
Customer cust[] = dao.findWhereRegionEquals( "US" );

Sample DAO Code

Here is some sample DAO code generated by FireStorm/DAO for a MySQL table called 'customer':

Data Transfer Object (DTO):Customer.java
Primary Key DTO:CustomerPk.java
DAO Interface:CustomerDao.java
DAO Implementation:CustomerDaoImpl.java

Additional Resources

FireStorm/DAO is a database access tool based on the Data Access Object design pattern.

Download FireStorm/DAO

CodeFutures provides a free program to analyze the performance of your MySQL database.

Free MySQL Performance Analysis

Read about how Database Sharding helps many major companies to linearly scale their database applications.

Request Database Sharding White Paper

dbShards economically scales large, high transaction volume databases using Database Sharding.

View Introduction to dbShards Video