Introduction of DAO design pattern in JavaBean __java

Source: Internet
Author: User
Tags getdate

I. Development framework of information System

Customer layer ——-display layer ——-business layer ——— data layer ——— database

1. Customer Layer: Customer layer is the client, simple is the browser.

2. Display layer: Jsp/servlet, for display to the browser.

3. Business layer: Integration of atomic operations in the data tier .

4. Data layer: For the database of atomic operation, add, delete, etc.


Second, DAO (Data Access Object) Introduction

The DAO is applied to the data-tier block, a class that accesses the database and operates on the database.


iii. structure of DAO design pattern

The DAO design pattern is generally divided into several classes:

1.VO (Value Object): A class that holds a single row of data for a Web page, such as a record, such as a Web page that displays a user's information, then this class is the user's class.

2.DatabaseConnection: Used to open and close the database.

3.DAO interface: For declaring operations on a database.

4.DAOImpl: The DAO interface must be implemented to implement the functions of the DAO interface, but does not include the opening and closing of the database.

5.DAOProxy: The DAO interface is also implemented, but it only needs to be Daoimpl, but includes the opening and closing of the database.

6.DAOFactory: Factory class, containing getinstance () create a proxy class.


iv. benefits of DAO

The advantage of DAO is that the interface provided to the user is only DAO interface, so if the user wants to add data, it is necessary to call the CREATE function and do not need the operation of the database.


Five, DAO package naming

For DAO, the name of the package and the naming of the class must be hierarchical.


vi. Analysis of examples

1.emp.java

[Java] View Plain copy print? package org.vo;   import java.util.*;   public class emp{       private int empno;       private string  ename;       private String job;        private Date hireDate;       private float sal;        public emp () {                   }       public int getempno () {            return empno;        }       public void setempno (int empno) {            this.empno = empno;       }       public string getename () {            return ename;       }        public void setename (string ename) {            this.ename = ename;       }        public date gethiredate () {           return  hireDate;       }       public void  Sethiredate (date hiredate) {           this.hiredate  = hireDate;       }       public  Float getsal () {           return sal;       }       public void setsal (float sal) {           this.sal = sal;       }        public string getjob () {            return job;       }        public void setjob (string job) {            this.job = job;       }  }  

Package org.vo;
Import java.util.*;
public class emp{
    private int empno;
    Private String ename;
    Private String job;
    Private Date hiredate;
    private float Sal;
    Public Emp () {

    } public
    int getempno () {return
        empno;
    }
    public void setempno (int empno) {
        this.empno = empno;
    }
    Public String Getename () {return
        ename;
    }
    public void Setename (String ename) {
        this.ename = ename;
    }
    Public Date Gethiredate () {return
        hiredate;
    }
    public void Sethiredate (Date hiredate) {
        this.hiredate = hiredate;
    }
    public float Getsal () {return
        sal;
    }
    public void Setsal (float sal) {
        this.sal = sal;
    }
    Public String Getjob () {return
        job;
    }
    public void Setjob (String job) {
        this.job = job;
    }
}
2.databaseconnection.java

[Java] View Plain copy print? package org.dbc;   import java.sql.*;   public class databaseconnection{        private Connection con = null;        private static final String DRIVER =  "Com.mysql.jdbc.Driver";        private static final String USER =  "root";        private static final String URL =  "jdbc:mysql:// Localhost:3306/mldn ";       private static final string pass  =  "12345";       public databaseconnection () throws exception {           class.forname (DRIVER);            con = drivermanager.getconnection (URL,USER,PASS);       }       public Connection  Getconnection () throws exception{           return  con;       }       public void close () throws exception{           if (con!=null) {               con.close ();            }       }  }  

Package org.dbc;
Import java.sql.*;
public class databaseconnection{
    private Connection con = null;
    private static final String DRIVER = "Com.mysql.jdbc.Driver";
    private static final String USER = "root";
    private static final String URL = "JDBC:MYSQL://LOCALHOST:3306/MLDN";
    Private static final String pass = "12345";
    Public DatabaseConnection () throws exception{
        Class.forName (DRIVER);
        con = drivermanager.getconnection (url,user,pass);
    }
    Public Connection getconnection () throws exception{return
        con;
    public void Close () throws exception{
        if (con!=null) {
            con.close ();
        }
    }
}
3.iempdao.java

[Java] view plain copy print?   Package Org.dao;   Import java.util.List;   Import org.vo.*;       Public interface iempdao{Public boolean docreate (EMP emp) throws Exception;       Public list<emp> FindAll () throws Exception;   Public Emp FindByID (int empno) throws Exception; }

Package Org.dao;
Import java.util.List;
Import org.vo.*;
Public interface iempdao{Public
    boolean docreate (EMP emp) throws Exception;
    Public list<emp> FindAll () throws Exception;
    Public Emp FindByID (int empno) throws Exception;
}
4.empdaoimpl.java

[Java] View Plain copy print? package org.dao.impl;   import org.dao.*;   import java.sql.*;   import org.vo.*;   import java.util.*;   public class empdaoimpl  implements iempdao{       private Connection con;       private PreparedStatement stat = null;        public empdaoimpl (Connection con) {            this.con = con;       }       public  boolean docreate (emp emp) throws exception{            String sql =  "Insert into emp (empno,ename,job,hiredate,sal)   VALUES (?,?,?,?,?) ";            stat = con.preParestatement (SQL);           stat.setint (1,emp.getEmpno ());            stat.setstring (2,emp.getename ());           stat.setstring (3,emp.getjob ());            stat.setdate (4,new java.sql.date (Emp.gethiredate (). GetTime ());           stat.setfloat (5,emp.getsal ());            int update = stat.executeupdate ();            if (update>0) {                return true;           }            else{                return false;           }       }       public list<emp> findall () throws  Exception{           String sql =  " Select empno,ename,job,hiredate,sal from emp ";            stat = con.preparestatement (SQL);            resultset rs = stat.executequery ();            Emp emp = null;           list<emp > list = new ArrayList<Emp> ();            while (Rs.next ()) {                int empNo = rs.getint (1);                string ename = rs.getstring (2);                string job = rs.getstring (3);                float sal = rs.getfloat (5);                emp = new emp ();               emp.setempno (empno);                emp.setename (ename);                emp.setjob (Job);                emp.sethiredate (Rs.getdate (4));                 emp.setsal (SAL);                list.add (EMP);            }           return list;        }       public emp findbyid (int empno) throws exception{            String sql =  "Select empno, Ename,job,hiredate,sal from emp where empno=? ";            stat = con.preparestatement (SQL);            stat.setint (1,empno);            resultset rs = stat.executequery ();            emp emp = null;           if (Rs.next ()) {                string ename = rs.getstring (2);                String job =  Rs.getstring (3);               float  sal = rs.getfloat (5);                emp = new emp ();                emp.setempno (empno);                emp.setename (ename);                emp.setjob (Job);                Emp.sethiredate (rs.getdAte (4));               emp.setsal (SAL);            }            return emp;       }  }  

Package Org.dao.impl;
Import org.dao.*;
Import java.sql.*;
Import org.vo.*;
Import java.util.*;
    public class Empdaoimpl implements iempdao{private Connection con;
    Private PreparedStatement stat = null;
    Public Empdaoimpl (Connection con) {this.con = con; public boolean docreate (EMP emp) throws exception{String sql = ' INSERT into Emp ' (empno,ename,job,hiredate,sal
        ) VALUES (?,?,?,?,?) ";
        Stat = con.preparestatement (SQL);
        Stat.setint (1,emp.getempno ());
        Stat.setstring (2,emp.getename ());
        Stat.setstring (3,emp.getjob ());
        Stat.setdate (4,new java.sql.Date (Emp.gethiredate (). GetTime ()));
        Stat.setfloat (5,emp.getsal ());
        int update = Stat.executeupdate ();
        if (update>0) {return true;
        } else{return false; } public list<emp> FindAll () throws exception{String sql = "Select Empno,ename,job,hiredate,sal FR
        OM EMP "; Stat = Con.preParestatement (SQL);
        ResultSet rs = Stat.executequery ();
        EMP emp = NULL;
        list<emp> list = new arraylist<emp> ();
            while (Rs.next ()) {int empno = Rs.getint (1);
            String ename = rs.getstring (2);
            String job = rs.getstring (3);
            float sal = rs.getfloat (5);
            EMP = new EMP ();
            Emp.setempno (EMPNO);
            Emp.setename (ename);
            Emp.setjob (Job);
            Emp.sethiredate (Rs.getdate (4));
            Emp.setsal (SAL);
        List.add (EMP);
    } return list; Public Emp FindByID (int empno) throws exception{String sql = ' Select empno,ename,job,hiredate,sal from Emp W
        Here empno=? ";
        Stat = con.preparestatement (SQL);
        Stat.setint (1,empno);
        ResultSet rs = Stat.executequery ();
        EMP emp = NULL;
            if (Rs.next ()) {String ename = rs.getstring (2);
            String job = rs.getstring (3); FloatSal = Rs.getfloat (5);
            EMP = new EMP ();
            Emp.setempno (EMPNO);
            Emp.setename (ename);
            Emp.setjob (Job);
            Emp.sethiredate (Rs.getdate (4));
        Emp.setsal (SAL);
    } return EMP; }
}

5.empdaoproxy.java

[Java] view plain copy print?   Package Org.dao.impl;   Import org.dao.*; Import java.sql.*;

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.