HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Data Access Object Model (Data Access Object Pattern) or DAO pattern for the low-level data access API or separated from the operation of advanced business services. The following is a participant in the Data Access Object pattern.
We will create aStudentobject model object or value object as.StudentDaodata access object interface.StudentDaoImplentity class is to achieve a data access object interface.DaoPatternDemo,our demonstration class to demonstrate the use ofStudentDaoData Access Object pattern of usage.
Create a value object.
Student.java
public class Student { private String name; private int rollNo; Student (String name, int rollNo) { this.name = name; this.rollNo = rollNo; } public String getName () { return name; } public void setName (String name) { this.name = name; } public int getRollNo () { return rollNo; } public void setRollNo (int rollNo) { this.rollNo = rollNo; } }
Create a data access object interface.
StudentDao.java
import java.util.List; public interface StudentDao { public List <Student> getAllStudents (); public Student getStudent (int rollNo); public void updateStudent (Student student); public void deleteStudent (Student student); }
Create entity class implements the above interfaces.
StudentDaoImpl.java
import java.util.ArrayList; import java.util.List; public class StudentDaoImpl implements StudentDao { // List is a database as a List <Student> students; public StudentDaoImpl () { students = new ArrayList <Student> (); Student student1 = new Student ( "Robert", 0); Student student2 = new Student ( "John", 1); students.add (student1); students.add (student2); } @Override public void deleteStudent (Student student) { students.remove (student.getRollNo ()); System.out.println ( "Student: Roll No" + student.getRollNo () + ", Deleted from database"); } // Retrieve the list of students from the database @Override public List <Student> getAllStudents () { return students; } @Override public Student getStudent (int rollNo) { return students.get (rollNo); } @Override public void updateStudent (Student student) { students.get (student.getRollNo ()) setName (student.getName ()).; System.out.println ( "Student: Roll No" + student.getRollNo () + ", Updated in the database"); } }
StudentDaoto demonstrate the use of the Data Access Object pattern of usage.
CompositeEntityPatternDemo.java
public class DaoPatternDemo { public static void main (String [] args) { StudentDao studentDao = new StudentDaoImpl (); // Output all students for (Student student: studentDao.getAllStudents ()) { System.out.println ( "Student: [RollNo:" + Student.getRollNo () + ", Name:" + student.getName () + "]"); } // Update Student Student student = studentDao.getAllStudents () get (0).; student.setName ( "Michael"); studentDao.updateStudent (student); // Get students studentDao.getStudent (0); System.out.println ( "Student: [RollNo:" + Student.getRollNo () + ", Name:" + student.getName () + "]"); } }
Verify output.
Student: [RollNo: 0, Name: Robert] Student: [RollNo: 1, Name: John] Student: Roll No 0, updated in the database Student: [RollNo: 0, Name: Michael]