标签:
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();
}
}
}
}
看一下具体的结构:
标签:
原文地址:http://blog.csdn.net/u012437355/article/details/46538687