• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data Access Object

 
bhumireddy reddy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34965
377
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhumireddy,
Welcome to JavaRanch!

Have you seen Sun's DAO description page yet? It has a good description and example.
 
ravi bhosale
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic