DAO后台程序中的真实实现类:DeptDAOImpl.java
package xs.mldn.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import xs.mldn.dao.IDeptDAO;
import xs.mldn.vo.Dept;
public class DeptDAOImpl implements IDeptDAO {
private Connection conn;
private PreparedStatement pstmt;
public DeptDAOImpl(Connection conn) {
this.conn = conn;
}
public boolean doCreate(Dept vo) throws Exception {
boolean flag = false;
String sql = "INSERT INTO dept(deptno,dname,loc) VALUES (?,?,?) ";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setInt(1, vo.getDeptno());
this.pstmt.setString(2, vo.getDname());
this.pstmt.setString(3, vo.getLoc());
if (this.pstmt.executeUpdate() > 0) {
flag = true;
}
return flag;
}
public boolean doRemove(Integer id) throws Exception {
boolean flag = false;
String sql = "DELETE FROM dept WHERE deptno=?";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setInt(1, id);
if (this.pstmt.executeUpdate() > 0) {
flag = true;
}
return flag;
}
public boolean doUpdate(Dept vo) throws Exception {
boolean flag = false;
String sql = "UPDATE dept SET dname=?,loc=? WHERE deptno=?";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setString(1, vo.getDname());
this.pstmt.setString(2, vo.getLoc());
this.pstmt.setInt(3, vo.getDeptno());
if (this.pstmt.executeUpdate() > 0) {
flag = true;
}
return flag;
}
public List<Dept> findAll(String keyWord) throws Exception {
List<Dept> all = new ArrayList<Dept>();
String sql = "SELECT deptno,dname,loc FROM dept WHERE deptno LIKE ? OR dname LIKE ? OR loc LIKE ? ";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setString(1, "%" + keyWord + "%");
this.pstmt.setString(2, "%" + keyWord + "%");
this.pstmt.setString(3, "%" + keyWord + "%");
ResultSet rs = this.pstmt.executeQuery();
while (rs.next()) {
Dept dept = new Dept();
dept.setDeptno(rs.getInt(1));
dept.setDname(rs.getString(2));
dept.setLoc(rs.getString(3));
all.add(dept);
}
return all;
}
public List<Dept> findAll(String keyWord, int currentPage, int lineSize)
throws Exception {
throw new Exception("此方法未实现!");
}
public Dept findById(Integer id) throws Exception {
Dept dept = null;
String sql = "SELECT deptno,dname,loc FROM dept WHERE deptno=? ";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setInt(1, id);
ResultSet rs = this.pstmt.executeQuery();
if (rs.next()) {
dept = new Dept();
dept.setDeptno(rs.getInt(1));
dept.setDname(rs.getString(2));
dept.setLoc(rs.getString(3));
}
return dept;
}
public long getAllCount(String keyWord) throws Exception {
throw new Exception("此方法未实现!") ;
}
}
DAO后台操作中的代理实现类:DeptDAOProxy.java
package xs.mldn.dao.proxy;
import java.util.List;
import xs.mldn.dao.IDeptDAO;
import xs.mldn.dao.impl.DeptDAOImpl;
import xs.mldn.dbc.DatabaseConnection;
import xs.mldn.vo.Dept;
public class DeptDAOProxy implements IDeptDAO {
private DatabaseConnection dbc = null;
private IDeptDAO dao = null;
public DeptDAOProxy() {
this.dbc = new DatabaseConnection();
this.dao = new DeptDAOImpl(this.dbc.getConnection());
}
public boolean doCreate(Dept vo) throws Exception {
boolean flag = false;
try {
if (this.dao.findById(vo.getDeptno()) == null) {
flag = this.dao.doCreate(vo);
}
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
return flag;
}
public boolean doRemove(Integer id) throws Exception {
boolean flag = false;
try {
flag = this.dao.doRemove(id);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
return flag;
}
public boolean doUpdate(Dept vo) throws Exception {
boolean flag = false;
try {
flag = this.dao.doUpdate(vo);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
return flag;
}
public List<Dept> findAll(String keyWord) throws Exception {
List<Dept> all = null;
try {
all = this.dao.findAll(keyWord);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
return all;
}
public List<Dept> findAll(String keyWord, int currentPage, int lineSize)
throws Exception {
List<Dept> all = null;
try {
all = this.dao.findAll(keyWord, currentPage, lineSize);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
return all;
}
public Dept findById(Integer id) throws Exception {
Dept dept = null;
try {
dept = this.dao.findById(id);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
return dept;
}
public long getAllCount(String keyWord) throws Exception {
long count = 0;
try {
count = this.dao.getAllCount(keyWord);
} catch (Exception e) {
throw e;
} finally {
this.dbc.close();
}
return count;
}
}