日期:2014-05-20  浏览次数:20739 次

DAO模式的优势在哪?
这是DAO模式的代码:
Java code
package cn.jbit.vo;

/**
 *  宠物类
 */
public class Pet {
    private int id;//宠物id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    
    private int masterId;//主人id
    public int getMasterId() {
        return masterId;
    }
    public void setMasterId(int masterId) {
        this.masterId = masterId;
    }
    
    private String name;//昵称
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    private int typeId;//类型id
    public int getTypeId() {
        return typeId;
    }
    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }
    
    private int health;//健康值
    public int getHealth() {
        return health;
    }
    public void setHealth(int health) {
        this.health = health;
    }
    
    private int love ;//亲密度
    public int getLove() {
        return love;
    }
    public void setLove(int love) {
        this.love = love;
    }
    
    private String adoptTime;//领养时间
    public String getAdoptTime() {
        return adoptTime;
    }
    public void setAdoptTime(String adoptTime) {
        this.adoptTime = adoptTime;
    }
    
    private int status;//状态
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
}



Java code

package cn.jbit.dao;

import java.util.List;

import cn.jbit.vo.Pet;

/**
 * 接口
 */
public interface IPetDAO {
    public boolean doCreate(Pet pet)throws Exception;
    public List<Pet>findInfo(int keyword)throws Exception;
}




Java code

package cn.jbit.dbc;

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

import org.apache.log4j.Logger;

/**
 *  数据库连接
 */
public class DataBaseConnection {
    //记录日志
    private Logger logger = Logger.getLogger(DataBaseConnection.class);
    private static final String driverClassName = "oracle.jdbc.driver.OracleDriver";  //驱动字符串
    private static final String url = "jdbc:oracle:thin:@127.0.0.1:1521:oracle10";  //连接字符串
    private static final String user = "admin";
    private static final String password = "admin";
    private Connection conn = null;
    
    public DataBaseConnection()throws Exception{
        Class.forName(driverClassName);
        this.conn = DriverManager.getConnection(url,user,password);
        logger.debug("连接成功");
    }
    
    public Connection getConnection(){
        return this.conn;
    }
    
    /**
     * 关闭连接
     */
    public void close(){
        if(null!=conn){
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println(e);
            }
        }
    }
}



Java code

package cn.jbit.factory;
import cn.jbit.Proxy.PetDaoProxy;
import cn.jbit.dao.IPetDAO;

public class factory {
    public static IPetDAO getIpetDAOinstance()throws
    Exception{
        return new PetDaoProxy();
    }
}



Java code

package cn.jbit.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.jbit.dao.IPetDAO;
import cn.jbit.vo.Pet;

public class PetDaoImpl implements IPetDAO {
    Connection conn = null;
    PreparedStatement patmt = null;
    ResultSet rs = null;
    
    public PetDaoImpl(Connection conn){
        this.conn = conn;
    }

    public boolean doCreate(Pet pet) throws Exception {
        boolean flag = false;
        String sql = "insert into pet values(pet_sep.nextval,?,?,?,?,?,sysdate,?)";
        this.patmt = this.conn.prepareStatement(sql);
        this.patmt.setInt(1, pet.getMasterId());
        this.patmt.setString(2, pet.getName());
        this.patmt.setInt(3,pet.getTypeId());
        this.patmt.setInt(4, pet.getHealth());
        this.patmt.setInt(5, pet.getLove());
        this.patmt.setInt(6, pet.getStatus());
        if(this.patmt.executeUpdate()>0){            
            System.out.println("插入成功");
            flag = true;
        } 
        return flag;
    }

    public List<Pet> findInfo(int keyword) throws Exception {
        List<Pet> list = new ArrayList<Pet>();
        String sql = "select id,master_id,name,health,love from pet where id = ?";
        this.patmt = this.conn.prepareStatement(sql);
        this.patmt.setInt(1, keyword);
        rs = this.patmt.executeQuery();
        Pet pet = new Pet();
        while(rs.next()){
            pet = new Pet();
            pet.setId(rs.getInt(1));
            pet.setMasterId(rs.getInt(2));
            pet.setName(rs.getString(3));
            pet.setHealth(rs.getInt(4));
            pet.setLove(rs.getInt(5));
        }
        list.add(pet);
        System.out.println("***狗狗***");
        System.out.println("编号\t主人编号\t名字\t健康值\t亲密度");
            System.out.print(pet.getId()+"\t");
            System.out.print(pet.getMasterId()+"\t");
            System.out.print(pet.getName()+"\t");
            System.out.print(pet.getHealth()+"\t");
            System.out.print(pet.getLove()+"\n");
        this.patmt.close();
        return list;
    }

}