Home Tutorial Java Jdbc DAO In Java

 
 

Share on Google+Share on Google+
DAO In Java
Posted on: June 25, 2013 at 12:00 AM
Advertisement
DAO in Java, section describes you the pattern for specifying the accessibility of data from the database.

DAO In Java

In this section we will read about the DAO in Java.

DAO is a pattern that separates the high level business logic from the data accessing operations. DAO allows you to write the code for working with the database's data. DAO makes your code complexity less compare to the code writing together business logic and data accessing operations. A Data Access Object pattern may have the following participants :

  • An Interface : A DAO interface may declare the important operations required to perform on Model Objects.
  • A Class : A DAO can be a concrete class that may implement the DAO interface or may define the operations required to perform on Model Objects. This class has the main role that plays for getting the data from datasource i.e. database, XML or any other storage mechanism.
  • Model Object : Model Object may be called a POJO class that has the data members and their setter/getter methods to store the data retrieved through the DAO Class.

How To Create A DAO In Java?

To create a DAO in Java we may include all/part of its participant discussed above. From the given participant we may or not create an interface we can directly define the operations in the concrete class that has to be perform. But, in my example I will include all of its participant i.e. I will create an interface, class and model object.

Model Object

Employee.java

package net.roseindia.dao;

public class Employee {

	private String name;
	private int id;
	
	public Employee(String name, int id) {
		super();
		this.name = name;
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}	
}

DAO Interface

EmployeeDao.java

package net.roseindia.dao;

import java.util.List;
public interface EmployeeDao {

	public List getAllEmployee();
	   public Employee getEmployee(int id);
	   public void updateEmployee(Employee employee);
	   public void deleteEmployee(Employee employee);
}

Concrete Class

EmployeeDaoImpl.java

package net.roseindia.dao;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDaoImpl implements EmployeeDao {
	
   //list is working as a database
   List employees;

   public EmployeeDaoImpl(){
      employees = new ArrayList();
      Employee employee1 = new Employee("Robert",0);
      Employee employee2 = new Employee("John",1);
      employees.add(employee1);
      employees.add(employee2);		
   }
   @Override
   public void deleteEmployee(Employee emp) {
      employees.remove(emp.getId());
      System.out.println("Employee: ID " + emp.getId() 
         +", deleted from database");
   }

   //retrieve list of Employees from the database
   @Override
   public List getAllEmployee() {
      return employees;
   }

   @Override
   public Employee getEmployee(int id) {
      return employees.get(id);
   }

   @Override
   public void updateEmployee(Employee emp) {
      employees.get(emp.getId()).setName(emp.getName());
      System.out.println("Employee: ID " + emp.getId() 
         +", updated in the database");
   }
}

Main Class

MainClass.java

package net.roseindia.dao;

public class MainClass {
	   public static void main(String[] args) {
	      EmployeeDao EmployeeDao = new EmployeeDaoImpl();

	      //print all Employees
	      for (Employee emp : EmployeeDao.getAllEmployee()) {
	         System.out.println("Employee: [Id : "
	            +emp.getId()+", Name : "+emp.getName()+" ]");
	      }


	      //update Employee
	      Employee emp =EmployeeDao.getAllEmployee().get(0);
	      emp.setName("Michael");
	      EmployeeDao.updateEmployee(emp);

	      //get the Employee
	      EmployeeDao.getEmployee(0);
	      System.out.println("Employee: [ID : "
	         +emp.getId()+", Name : "+emp.getName()+" ]");		
	   }
	}

Output :

When you will compile and execute the MainClass.java file then the output will be as follows :

Download Source Code

Advertisement

Related Tags for DAO In Java:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 25, 2013

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com