程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SyBase數據庫 >> SyBase教程 >> 課程設計小知識

課程設計小知識

編輯:SyBase教程

課程設計小知識


DAO

DAO是Data Access Object三個首字母的組合,是和數據庫打交道的,夾在業務邏輯與數據資源中間。一個標准的DAO模式包含:
(1)VO:數據傳輸對象,一般和數據庫中的一張數據表對應(一個實例相當於數據表中的一條記錄,具體看例子比較好理解)
(2)DAO接口:一個接口,聲明包含的需要的操作
(3)DAOImpl:DAO接口的具體實現類,只負責實現DAO接口聲明的功能,不包括數據庫的打開和關閉
(4)DAOProxy:代理,借助DAOImpl實現了DAO接口,但也包括數據庫的打開和關閉
(5)DAOFactory:DAO工廠,就是提供dao的地方,只包含public類型的返回dao類型的getInstance()靜態方法,該方法提供DAOProxy實例,並返回
(6)DataBaseConnection:負責打開關閉數據庫。
例子:比如我們要操作名稱為program的數據庫中的一張名為reader的表,按照上面的步驟,寫出如下代碼:
(1)在VO包裡寫出Reader類:

/*只用寫出類中的成員變量(也叫屬性),對應數據庫中的字段,後面的get,set,已經構造完全自動生成,一個該類實例可以表示一條記錄*/
package vo;
public class Reader {
    private int readerid;//讀者id號
    private String cardid;//借書證id
    private String readername;//讀者姓名
    private String password;//密碼
    private String sex;//讀者性別
    private String idcard;//身份證
    private String homeaddress;//家庭住址
    private String phone;//電話
    private String jobaddress;//工作單位地址
    private int booksum;//可以借書數量
    private String carddate;//辦證日期
    private String abatedate;//失效日期
    private int borrowersum;//以借書數量
    private String xueli;//學歷
    private String remark;//備注信息
    public Reader() {
        super();
    }

    public Reader(String cardid, String readername, String password,
            String sex, String idcard, String homeaddress, String phone,
            String jobaddress, int booksum, String carddate, String abatedate,
            int borrowersum, String xueli, String remark) {
        super();
        this.cardid = cardid;
        this.readername = readername;
        this.password = password;
        this.sex = sex;
        this.idcard = idcard;
        this.homeaddress = homeaddress;
        this.phone = phone;
        this.jobaddress = jobaddress;
        this.booksum = booksum;
        this.carddate = carddate;
        this.abatedate = abatedate;
        this.borrowersum = borrowersum;
        this.xueli = xueli;
        this.remark = remark;
    }

    public Reader(int readerid,
            String cardid, 
            String readername,
            String password, 
            String sex, 
            String idcard, 
            String homeaddress,
            String phone, 
            String jobaddress, 
            int booksum, 
            String carddate,
            String abatedate, 
            int borrowersum, 
            String xueli, 
            String remark) {
        super();
        this.readerid = readerid;
        this.cardid = cardid;
        this.readername = readername;
        this.password = password;
        this.sex = sex;
        this.idcard = idcard;
        this.homeaddress = homeaddress;
        this.phone = phone;
        this.jobaddress = jobaddress;
        this.booksum = booksum;
        this.carddate = carddate;
        this.abatedate = abatedate;
        this.borrowersum = borrowersum;
        this.xueli = xueli;
        this.remark = remark;
    }

    public int getReaderid() {
        return readerid;
    }
    public void setReaderid(int readerid) {
        this.readerid = readerid;
    }
    public String getCardid() {
        return cardid;
    }
    public void setCardid(String cardid) {
        this.cardid = cardid;
    }
    public String getReadername() {
        return readername;
    }
    public void setReadername(String readername) {
        this.readername = readername;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getIdcard() {
        return idcard;
    }
    public void setIdcard(String idcard) {
        this.idcard = idcard;
    }
    public String getHomeaddress() {
        return homeaddress;
    }
    public void setHomeaddress(String homeaddress) {
        this.homeaddress = homeaddress;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getJobaddress() {
        return jobaddress;
    }
    public void setJobaddress(String jobaddress) {
        this.jobaddress = jobaddress;
    }
    public int getBooksum() {
        return booksum;
    }
    public void setBooksum(int booksum) {
        this.booksum = booksum;
    }
    public String getCarddate() {
        return carddate;
    }
    public void setCarddate(String carddate) {
        this.carddate = carddate;
    }
    public String getAbatedate() {
        return abatedate;
    }
    public void setAbatedate(String abatedate) {
        this.abatedate = abatedate;
    }
    public String getXueli() {
        return xueli;
    }
    public void setXueli(String xueli) {
        this.xueli = xueli;
    }
    public int getBorrowersum() {
        return borrowersum;
    }
    public void setBorrowersum(int borrowersum) {
        this.borrowersum = borrowersum;
    }
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }

}

(2)在dao包中聲明具體要實現的操作

/*聲明具體要實現的操作*/
package dao;
import java.util.List;
import vo.Reader;
public interface ReaderDAO {
    /*向reader表中插入一條記錄*/
    public int add(Reader reader)throws Exception;
    /*向reader表中刪除一條記錄*/
    public int delete(int id)throws Exception;
    /*向reader表中修改一條記錄*/
    public int update(Reader reader)throws Exception;
    /*向reader表中根據id搜索一條記錄*/
    public Reader findByID(int id)throws Exception;
    /*向reader表中查詢所有記錄*/
    public List<Reader> findAll()throws Exception;
}

(3)在impl包裡寫出如下實現:

package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import vo.Reader;
import dao.ReaderDAO;

public class ReaderDAOImpl implements ReaderDAO {

    private Connection con = null;//數據庫連接,由外部提供,只負責聲明操作的實現
    public ReaderDAOImpl(Connection con) {
        super();
        this.con = con;
    }
    @Override
    public int add(Reader reader) throws Exception {
        // TODO Auto-generated method stub
        int count = 0;
        String sql = "INSERT reader (readerid,cardid,readername,password,sex,idcard,homeaddress,phone,jobaddress,booksum,carddate,abatedate,borrowersum,xueli,remark) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement pstmt = null;
        try {
            pstmt = this.con.prepareStatement(sql);
            pstmt.setInt(1, reader.getReaderid());
            pstmt.setString(2, reader.getCardid());
            pstmt.setString(3, reader.getReadername());
            pstmt.setString(4, reader.getPassword());
            pstmt.setString(5, reader.getSex());
            pstmt.setString(6, reader.getIdcard());
            pstmt.setString(7, reader.getHomeaddress());
            pstmt.setString(8, reader.getPhone());
            pstmt.setString(9, reader.getJobaddress());
            pstmt.setInt(10, reader.getBooksum());
            pstmt.setString(11, reader.getCarddate());
            pstmt.setString(12, reader.getAbatedate());
            pstmt.setInt(13,reader.getBorrowersum());
            pstmt.setString(14,reader.getXueli());
            pstmt.setString(15, reader.getRemark());

            count = pstmt.executeUpdate();//執行更新
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            pstmt.close();
        }
        return count;
    }

    @Override
    public int delete(int id) throws Exception {
        // TODO Auto-generated method stub
        int count = 0;
        String sql = "DELETE FROM reader WHERE readerid=?";
        PreparedStatement pstmt = null;
        try {

            pstmt = this.con.prepareStatement(sql);
            pstmt.setInt(1, id);
            count = pstmt.executeUpdate();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            pstmt.close();
        }
        return count;
    }

    @Override
    public int update(Reader reader) throws Exception {
        // TODO Auto-generated method stub
        int count = 0;
        String sql = "UPDATE reader SET cardid=?,readername=?,password=?,sex=?,idcard=?,homeaddress=?,phone=?,jobaddress=?,booksum=?,carddate=?,abatedate=?,borrowersum=?,xueli=?,remark=? WHERE readerid=?";
        PreparedStatement pstmt = null;
        try {
            pstmt = this.con.prepareStatement(sql);
            pstmt.setString(1, reader.getCardid());
            pstmt.setString(2, reader.getReadername());
            pstmt.setString(3, reader.getPassword());
            pstmt.setString(4, reader.getSex());
            pstmt.setString(5, reader.getIdcard());
            pstmt.setString(6, reader.getHomeaddress());
            pstmt.setString(7, reader.getPhone());
            pstmt.setString(8, reader.getJobaddress());
            pstmt.setInt(9, reader.getBooksum());
            pstmt.setString(10, reader.getCarddate());
            pstmt.setString(11, reader.getAbatedate());
            pstmt.setInt(12, reader.getBorrowersum());
            pstmt.setString(13,reader.getXueli());
            pstmt.setString(14,reader.getRemark());
            pstmt.setInt(15, reader.getReaderid());
            count = pstmt.executeUpdate();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            pstmt.close();
        }
        return count;
    }

    @Override
    public Reader findByID(int id) throws Exception {
        // TODO Auto-generated method stub
        Reader reader = null;
        String sql = "SELECT * FROM reader WHERE readerid=?";
        PreparedStatement pstmt = null;
        try {
            pstmt = this.con.prepareStatement(sql);
            pstmt.setInt(1, id);
            ResultSet set = pstmt.executeQuery();
            //System.out.println(set);
            if(set.next()){
                reader = new Reader();
                reader.setReaderid(id);
                reader.setCardid(set.getString(2));
                reader.setReadername(set.getString(3));
                reader.setPassword(set.getString(4));
                reader.setSex(set.getString(5));
                reader.setIdcard(set.getString(6));
                reader.setHomeaddress(set.getString(7));
                reader.setPhone(set.getString(8));
                reader.setJobaddress(set.getString(9));
                reader.setBooksum(set.getInt(10));
                reader.setCarddate(set.getString(11));
                reader.setAbatedate(set.getString(12));
                reader.setBorrowersum(set.getInt(13));
                reader.setXueli(set.getString(14));
                reader.setRemark(set.getString(15));
                set.close();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            pstmt.close();
        }
        return reader;
    }

    @Override
    public List<Reader> findAll() throws Exception {
        // TODO Auto-generated method stub
        List<Reader> readers = null;
        Reader reader = null;
        String sql = "SELECT * FROM reader";
        PreparedStatement pstmt = null;
        try {
            pstmt = this.con.prepareStatement(sql);
            ResultSet set = pstmt.executeQuery();
            if(set!=null){
                readers = new ArrayList<Reader>();
                while(set.next()){
                    reader = new Reader();
                    reader.setReaderid(set.getInt(1));
                    reader.setCardid(set.getString(2));
                    reader.setReadername(set.getString(3));
                    reader.setPassword(set.getString(4));
                    reader.setSex(set.getString(5));
                    reader.setIdcard(set.getString(6));
                    reader.setHomeaddress(set.getString(7));
                    reader.setPhone(set.getString(8));
                    reader.setJobaddress(set.getString(9));
                    reader.setBooksum(set.getInt(10));
                    reader.setCarddate(set.getString(11));
                    reader.setAbatedate(set.getString(12));
                    reader.setBorrowersum(set.getInt(13));
                    reader.setXueli(set.getString(14));
                    reader.setRemark(set.getString(15));
                    readers.add(reader);
                }
                set.close();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            pstmt.close();
        }
        return readers;
    }

}

(4)proxy包中寫出dao的代理

package dao.proxy;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import vo.Reader;
import dao.ReaderDAO;
import dao.impl.ReaderDAOImpl;
import dbc.DataBaseConnection;

public class ReaderDAOProxy implements ReaderDAO {

    private DataBaseConnection dbc = null;//數據庫連接
    private Connection con = this.dbc.getConnection();
    private ReaderDAO dao = null;

    public ReaderDAOProxy(){
        super();
        dbc = new DataBaseConnection();
        con = dbc.getConnection();
        dao = new ReaderDAOImpl(con);
    }
    @Override
    public int add(Reader reader) throws Exception {
        // TODO Auto-generated method stub
        int count = 0;
        if(dao.findByID(reader.getReaderid())==null){
            count = dao.add(reader);
        }
        return count;
    }
    @Override
    public int delete(int id) throws Exception {
        // TODO Auto-generated method stub
        return dao.delete(id);
    }

    @Override
    public int update(Reader reader) throws Exception {
        // TODO Auto-generated method stub
        return dao.update(reader);
    }

    @Override
    public Reader findByID(int id) throws Exception {
        // TODO Auto-generated method stub
        return dao.findByID(id);
    }

    @Override
    public List<Reader> findAll() throws Exception {
        // TODO Auto-generated method stub
        return dao.findAll();
    }
    public void close(){
        try {
            if(con != null)
                con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

(5)在factory包中寫出工廠類

package dao.factory;
import dao.ReaderDAO;
import dao.proxy.ReaderDAOProxy;
public class DAOFactory {
    /*得到reader表的操作實例*/
    public static ReaderDAO getReaderDAOInstance(){
        return new ReaderDAOProxy();
    }
}

(6)數據庫的鏈接

package dbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/* 連接book數據庫
 * 對外提供打開連接,和關閉連接操作
 * */
public class DataBaseConnection {
    private static final String DRIVER = "com.mysql.jdbc.Driver";//數據庫驅動
    private static final String URL = "jdbc:mysql://localhost/program";//URL
    private static final String USER = "root";//數據庫用戶名
    private static final String PASSWORD = "root";//數據庫密碼
    private Connection con = null;//數據庫連接引用聲明
    /*構造函數,完成數據庫連接的生成*/
    public DataBaseConnection() {
        try {
            //注冊驅動
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            System.err.println("注冊驅動失敗!");
        }
        try {
            //實例化連接
            con = DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.err.println("實例化連接失敗!");
        }
    }
    /*得到數據庫連接*/
    public Connection getConnection(){
        return this.con;
    }
    /*關閉數據庫連接*/
    public void close(){
        if(this.con!=null){
            try {
                this.con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

看一下具體的結構:
結構

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved