JSP+DAO开发模式对列表的查询、更改等。

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;
}
}




转载于:https://my.oschina.net/u/1259702/blog/170861

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值