(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 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();
}
}
}
}
看一下具体的结构:

课程设计小知识(四)
tRemark(set.getString(15));
readers.add(reader);
}
set.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
pstmt.close();
}
return readers;
}
}