File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Data Access Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Data Access Object" Watch "Data Access Object" New topic

Data Access Object

bhumireddy reddy

Joined: Jun 17, 2006
Posts: 1
Hi to all,

Can any one help me by giving the DAO example code. and let me know the process of dao.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33106

Welcome to JavaRanch!

Have you seen Sun's DAO description page yet? It has a good description and example.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
ravi bhosale

Joined: Jun 17, 2006
Posts: 5
Hi following code shows abstraction between business layer and data access layer. data access can be any like using JDBC or Hibernate.

The DAO Factory
public abstract class DAOFactory
public static final DAOFactory HIBERNATE =
new com.vmware.vcp.dao.hibernate.HibernateDAOFactory();

public static final DAOFactory JDBC =
new com.vmware.vcp.dao.jdbc.JDBCDAOFactory();

public static final DAOFactory DEFAULT = HIBERNATE;
public static final DAOFactory getDAOFactory()
return DEFAULT;

public abstract CustomerDAO getCustomerDAO();

The Hibernate DAO Factory
public class HibernateDAOFactory extends DAOFactory
public CustomerDAO getCustomerDAO()
return new CustomerDAOHibernate(getCurrentSession(Constants.Hibernate.MASTER_SESSION_FACTORY_NAME));

The Customer DAO interface
public interface CustomerDAO {
public Customer findCustomerForUserId(String id, boolean lock);

The concrete impl of customer dao
public class CustomerDAOHibernate implements CustomerDAO
public Customer findCustomerForUserId(String id, boolean lock)
// Database access code

Business layer code

DAOFactory df = DAOFactory.getDAOFactory();
CustomerDAO custDao = df.getCustomerDAO();
custDao.findCustomerForUserId(UserId, true);
I agree. Here's the link:
subject: Data Access Object
It's not a secret anymore!