0% found this document useful (0 votes)
178 views4 pages

Java Data Access Object Pattern Guide

The Data Access Object (DAO) pattern separates the application's data access logic from the business logic. It provides a way to access and manipulate data in a database in an object-oriented way. The key participants are: 1. A DAO interface that defines the data access operations. 2. A concrete DAO class that implements the interface and interacts with the database. 3. A model or value object that represents the data and acts as a data container. The DAO pattern is demonstrated by creating Student model objects, a StudentDAO interface, a StudentDAOImpl class implementing the interface, and a DaoPatternDemo class that uses the DAO to access student data.

Uploaded by

maddalena pozzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
178 views4 pages

Java Data Access Object Pattern Guide

The Data Access Object (DAO) pattern separates the application's data access logic from the business logic. It provides a way to access and manipulate data in a database in an object-oriented way. The key participants are: 1. A DAO interface that defines the data access operations. 2. A concrete DAO class that implements the interface and interacts with the database. 3. A model or value object that represents the data and acts as a data container. The DAO pattern is demonstrated by creating Student model objects, a StudentDAO interface, a StudentDAOImpl class implementing the interface, and a DaoPatternDemo class that uses the DAO to access student data.

Uploaded by

maddalena pozzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Access Object Pattern

Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or
operations from high level business services. Following are the participants in Data Access Object
Pattern.

Data Access Object Interface - This interface defines the standard operations to be
performed on a model object(s).

Data Access Object concrete class - This class implements above interface. This class is
responsible to get data from a data source which can be database / xml or any other storage
mechanism.
Model Object or Value Object - This object is simple POJO containing get/set methods to
store data retrieved using DAO class.

Implementation

We are going to create a Student object acting as a Model or Value [Link] is Data Access
Object [Link] is concrete class implementing Data Access Object Interface.
DaoPatternDemo, our demo class, will use StudentDao to demonstrate the use of Data Access Object
pattern.

Step 1

Create Value Object.


[Link]
public class Student {
private String name;
private int rollNo;

Student(String name, int rollNo){


[Link] = name;
[Link] = rollNo;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public int getRollNo() {


return rollNo;
}

public void setRollNo(int rollNo) {


[Link] = rollNo;
}
}

Step 2

Create Data Access Object Interface.


[Link]

import [Link];

public interface StudentDao {


public List<Student> getAllStudents();
public Student getStudent(int rollNo);
public void updateStudent(Student student);
public void deleteStudent(Student student);
}

Step 3

Create concrete class implementing above interface.


[Link]

import [Link];
import [Link];

public class StudentDaoImpl implements StudentDao {

//list is working as a database


List<Student> students;

public StudentDaoImpl(){
students = new ArrayList<Student>();
Student student1 = new Student("Robert",0);
Student student2 = new Student("John",1);
[Link](student1);
[Link](student2);
}
@Override
public void deleteStudent(Student student) {
[Link]([Link]());
[Link]("Student: Roll No " + [Link]() + ", deleted from
}

//retrive list of students from the database


@Override
public List<Student> getAllStudents() {
return students;
}

@Override
public Student getStudent(int rollNo) {
return [Link](rollNo);
}

@Override
public void updateStudent(Student student) {
[Link]([Link]()).setName([Link]());
[Link]("Student: Roll No " + [Link]() + ", updated in th
}
}

Step 4
Use the StudentDao to demonstrate Data Access Object pattern usage.

[Link]

public class DaoPatternDemo {


public static void main(String[] args) {
StudentDao studentDao = new StudentDaoImpl();

//print all students


for (Student student : [Link]()) {
[Link]("Student: [RollNo : " + [Link]() + ", Name : "
}

//update student
Student student =[Link]().get(0);
[Link]("Michael");
[Link](student);
//get the student
[Link](0);
[Link]("Student: [RollNo : " + [Link]() + ", Name : " +
}
}

Step 5
Verify the output.

Student: [RollNo : 0, Name : Robert ]


Student: [RollNo : 1, Name : John ]
Student: Roll No 0, updated in the database
Student: [RollNo : 0, Name : Michael ]

You might also like