DAO
===
下面通过简单实例演示如何封装常用数据库操作类Dao
项目目录结构如下图
1.编写实体类:Emp
/**
-
1.通常实体类和表名一致
-
2.通常该类中属性名和字段名一致
-
3.通常该类中属性都使用封装类型
-
@author Cher_du
*/
public class Emp implements Serializable{
private static final long serialVersionUID = 1L;
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private Integer deptno;
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Double getComm() {
return comm;
}
public void setComm(Double comm) {
this.comm = comm;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
}
2.编写数据库操作类EmpDao:【封装CURD】
public class EmpDao {
//查询所有
public List findAll(){
Connection conn = null;
List empList = new ArrayList();
try {
conn = DBUtil.getConnection();
String sql = "select * from emp ";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Emp e =new Emp();
e.setEmpno(rs.getInt(“empno”));
e.setEname(rs.getString(“ename”));
e.setJob(rs.getString(“job”));
e.setMgr(rs.getInt(“mgr”));
e.setHiredate(rs.getDate(“hiredate”));
e.setSal(rs.getDouble(“sal”));
e.setComm(rs.getDouble(“comm”));
e.setDeptno(rs.getInt(“deptno”));
empList.add(e);
}
return empList;
} catch (SQLException e) {
e.printStackTrace();
}
return empList;
}
public Emp findById(int id){
Connection conn =null;
try {
conn = DBUtil.getConnection();
String sql = "select * from emp "
+“where empno =?”;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if(rs.next()){
Emp e =new Emp();
e.setEmpno(rs.getInt(“empno”));
e.setEname(rs.getString(“ename”));
e.setJob(rs.getString(“job”));
e.setMgr(rs.getInt(“mgr”));
e.setHiredate(rs.getDate(“hiredate”));
e.setSal(rs.getDouble(“sal”));
e.setComm(rs.getDouble(“comm”));
e.setDeptno(rs.getInt(“deptno”));
return e;
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(“根据ID查询员工失败!”,e);
}finally{
DBUtil.close(conn);
}
return null;
}
public boolean save(Emp emp){
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = “insert into emp values(”
+“?,?,?,?,?,?,?,?)”;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, emp.getEmpno());
ps.setString(2, emp.getEname());
ps.setString(3, emp.getJob());
ps.setInt(4, emp.getMgr());
ps.setDate(5, emp.getHiredate());
ps.setDouble(6,emp.getSal());
ps.setDouble(7, emp.getComm());
ps.setInt(8, emp.getDeptno());
int count = ps.executeUpdate();
if(count>0){
return true;
}else{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(“保存员工失败!”,e);
}finally{
DBUtil.close(conn);
}
}
public void update(Emp emp){
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "update emp set "
+“ename =?,”
+“job =?,”
+“mgr =?,”
+“hiredate =?,”
+“sal =?,”
+“comm =?,”
+"deptno =? "
+“where empno=?”;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, emp.getEname());
ps.setString(2, emp.getJob());
ps.setInt(3, emp.getMgr());
ps.setDate(4, emp.getHiredate());
ps.setDouble(5,emp.getSal());
ps.setDouble(6, emp.getComm());
ps.setInt(7, emp.getDeptno());
ps.setInt(8, emp.getEmpno());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(“修改员工失败!”,e);
}finally{
DBUtil.close(conn);
}
}
public boolean delete(int id){