The DAO pattern used in the ancient poetry MySQL database

Source: Internet
Author: User

I. What is DAO mode

DAO (Data Access Object Pattern) separates the lower-level data manipulation APIs from the business logic layer in the upper layer, which mainly involves the following sections:

1.Data Access Object Interface

Defines the standard operating interface on the model object.

2.Data Access Object Concrete Class

The interface is implemented in 1, which is responsible for manipulating data from database or XML.

3.Model object or Value object

A simple Pojo object.

Two. DAO Pattern example
Model or Value object:student

Data Access Object Interface:studentdao

Concrete class Implement Data Access Object Interface:studentdaoimpl

Studentdao is used to show how to use the DAO pattern.

Step1

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;   }}

Step2

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);

Step3

Concrete class Implemting above interface

Studentdaoimpl.java

Importjava.util.ArrayList;Importjava.util.List; Public classStudentdaoimplImplementsStudentdao {//list is working as a databaseList<student>students;  PublicStudentdaoimpl () {Students=NewArraylist<student>(); Student Student1=NewStudent ("Robert", 0); Student Student2=NewStudent ("John", 1);      Students.add (STUDENT1);           Students.add (Student2); } @Override Public voiddeletestudent (Student Student) {students.remove (Student.getrollno ()); System.out.println ("Student:roll No" + student.getrollno () + ", deleted from database"); }   //retrive List of students from the database@Override PublicList<student>getallstudents () {returnstudents; } @Override PublicStudent Getstudent (intRollno) {      returnStudents.get (Rollno); } @Override Public voidupdatestudent (Student Student) {students.get (Student.getrollno ()). SetName (Student.getname ()); System.out.println ("Student:roll No" + student.getrollno () + ", updated in the database"); }}

Step4

Use Studentdao to demonstrate DAO Pettern

public class Daopatterndemo {public   static void Main (string[] args) {      Studentdao Studentdao = new Studentdaoimpl ( );      Print 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 the student      studentdao.getstudent (0);      System.out.println ("Student: [Rollno:" + student.getrollno () + ", Name:" + student.getname () + "]");}   }

The DAO pattern used in the ancient poetry MySQL database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.