aspose file tools*
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
Author

Data Access Object

bhumireddy reddy
Greenhorn

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
Marshal

Joined: May 26, 2003
Posts: 30767
    
156

Bhumireddy,
Welcome to JavaRanch!

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
ravi bhosale
Greenhorn

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);
 
jQuery in Action, 2nd edition
 
subject: Data Access Object