I. Framework for the development of information systems
Client layer-------Display layer-------Business layer---------data layer---------Database
1. Customer Layer: Client layer is client, simple is browser.
2. Display layer: Jsp/servlet for display to the browser.
3. Business layer: Consolidates the atomic operations of the data layer .
4. Data layer: Atomic operations for databases, additions, deletions, etc.;
Second, DAO (Data Access Object) Introduction
DAO is applied to the data layer, which is used to access the database, the class that operates on the database.
Third, the structure of DAO design pattern
DAO design patterns generally fall into several categories:
1.VO (Value Object): A row of data for a Web page is a class of records, such as a Web page to display a user's information, then this class is the user's class.
2.DatabaseConnection: Used to open and close the database.
3.DAO interface: Used to declare operations for a database.
4.DAOImpl: DAO interface must be implemented, real function of DAO interface is implemented, but the opening and closing of database is not included.
5.DAOProxy: Also implements the DAO interface. But you just need to use Daoimpl. However, it contains the opening and closing of the database.
6.DAOFactory: Factory class. Contains getinstance () to create a proxy class.
Iv. Advantages of DAO
The advantage of DAO is that the interface provided to the user is only a DAO interface. So assuming that the user wants to add data, just call the CREATE function. The operation of the database is not required.
Five, DAO package naming
For DAO, the naming of packages and the naming of classes must be hierarchical.
Six, the case analysis
1.emp.java
Package Org.vo;import java.util.*;p ublic 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
Package Org.dbc;import java.sql.*;p ublic class databaseconnection{private Connection con = null;private static final Stri ng DRIVER = "Com.mysql.jdbc.Driver";p rivate static final String USER = "root";p rivate static final String URL = "jdbc:mysq L://localhost:3306/mldn ";p rivate static final String PASS =" 12345 ";p ublic 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
Package Org.dao;import java.util.list;import org.vo.*;p ublic 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
Package Org.dao.impl;import org.dao.*;import java.sql.*;import org.vo.*;import java.util.*;p ublic 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.P Reparestatement (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;}}
5.empdaoproxy.java
Package Org.dao.impl;import org.dao.*;import java.sql.*;import org.vo.*;import java.util.*;import org.dbc.*;p ublic Class Empdaoproxy implements Iempdao{private databaseconnection dbc;private Iempdao dao = Null;public empdaoproxy () Throws Exception{dbc = new DatabaseConnection ();d ao = new Empdaoimpl (Dbc.getconnection ());} public boolean docreate (EMP emp) throws Exception{boolean flag = False;if (Dao.findbyid (Emp.getempno ()) ==null) {flag = Dao.docreate (EMP);} Dbc.close (); return flag;} Public list<emp> FindAll () throws exception{list<emp>list = Dao.findall ();d bc.close (); return List;} Public emp FindByID (int empno) throws exception{emp emp = Dao.findbyid (empno);d bc.close (); return Emp;}}
6.daofactory.java
Package Org.dao.factory;import org.dao.*;import java.sql.*;import org.vo.*;import java.util.*;import org.dbc.*; Import org.dao.impl.*;p ublic class Daofactory{public static Iempdao getinstance () {Iempdao DAO = Null;try{dao = new EMPDAOP Roxy ();} catch (Exception e) {e.printstacktrace ();} return DAO;}}
7.testdao.java
Package Org.dao.test;import org.dao.factory.*;import org.vo.*;import org.dao.*;p ublic class testdao{public static void Main (String args[]) throws exception{emp emp = null;for (int i=0;i<5;i++) {emp = new emp (); Emp.setempno (i); emp.setename ("xiazdong-" +i); Emp.setjob ("stu-" +i); Emp.sethiredate (new Java.util.Date ()); Emp.setsal (500*i);D Aofactory.getinstance (). docreate (EMP);}}}
By the DAO design mode, the JSP can mask the operation of the database connection, and arrive JSP is only responsible for displaying the result.
Introduction to JavaBean in DAO design mode