DAO in Java, section describes you the pattern for specifying the accessibility of data from the database.
DAO in Java, section describes you the pattern for specifying the accessibility of data from the database.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 :
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 ListgetAllEmployee(); 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 Listemployees; 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 :