package com.hyxy.dao.impl;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.hyxy.dao.EmpDao;
import com.hyxy.entity.Emp;
import com.hyxy.util.DBUtil;
/**
* 编写接口EmpDao的实现类,实现持久层的
* 业务逻辑
*/
public class EmpDaoImpl implements EmpDao{
@Override
public void saveEmp(Emp emp) {
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "insert into emp (ename,job,mgr,hiredate,sal,comm,deptno)values(?,?,?,?,?,?,?)";
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.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn);
}
}
@Override
public void delEmp(Integer empno) {
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "delete from emp where empno=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, empno);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn);
}
}
@Override
public void updateEmp(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 (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn);
}
}
@Override
public Emp findById(Integer empno) {
Emp emp = null;
Connection conn = null;
try {
//获取连接对象
conn = DBUtil.getConnection();
String sql = "select * from emp where empno=?";
//获取预编译对象
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, empno);
//执行sql语句,处理结果集
ResultSet rs = ps.executeQuery();
if(rs.next()) {
String ename = rs.getString("ename");
String job = rs.getString("job");
int mgr = rs.getInt("mgr");
Date hiredate = rs.getDate("hiredate");
double sal = rs.getDouble("sal");
double comm = rs.getDouble("comm");
int deptno = rs.getInt("deptno");
//将这些数据封装成一个Emp对象
emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn);
}
return emp;
}
@Override
public List<Emp> findAll() {
Connection conn = null;
List<Emp> emps = new ArrayList<Emp>();
try {
conn = DBUtil.getConnection();
String sql = "select * from emp";
PreparedStatement ps = conn.prepareStatement(sql);
/*
* 4:处理结果集
* executeQuery():将查询的N条记录
* 封装到ResultSet类型中
* 此类型与迭代器相似,我们应该
* 先询问是有下一条记录(boolean next())
* 如果有,光标自动移至下一行,我们就
* 可以对此行进行处理
*/
ResultSet rs = ps.executeQuery();
while(rs.next()) {//询问
//进入循环体,说明有下一行,并且光标已经移至下一行了。
/*
* jdbc的结果集类型中为表中字段提供了
* 两个重载方法
* 如: int getInt(int columnIndex)
* int getInt(String columnName);
* 每种类型都有两个方法,如上。
*/
int empno = rs.getInt(1);//
String ename = rs.getString(2);
String job = rs.getString(3);
int mgr = rs.getInt(4);
Date hiredate = rs.getDate("hiredate");
double sal = rs.getDouble("sal");
double comm = rs.getDouble("comm");
int deptno = rs.getInt("deptno");
//封装成对象
Emp emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
//添加到集合中
emps.add(emp);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.closeConnection(conn);
}
return emps;
}
@Override
public List<Emp> findByPage(int page, int size) {
Connection conn = null;
List<Emp> emps = new ArrayList<Emp>();
try {
//获取连接对象
conn = DBUtil.getConnection();
String sql = "select * from emp order by empno limit ?,?";
//String sql1 = "select * from emp where empno>((?-1)*?) order by empno limit ?";
//获取预编译对象
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, (page-1)*size);
ps.setInt(2, size);
//执行sql语句,处理结果集
ResultSet rs = ps.executeQuery();
Emp emp = null;
while(rs.next()) {
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
String job = rs.getString("job");
int mgr = rs.getInt("mgr");
Date hiredate = rs.getDate("hiredate");
double sal = rs.getDouble("sal");
double comm = rs.getDouble("comm");
int deptno = rs.getInt("deptno");
//封装成Emp对象
emp = new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
//添加到集合中
emps.add(emp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn);
}
return emps;
}
@Override
public int totalPage(int size) {
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "select count(*) from emp";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
int lines = -1;
if(rs.next()) {
lines = rs.getInt(1);
}
//计算总页数
int totalPage = lines%size==0?lines/size:lines/size+1;
return totalPage;
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConnection(conn);
}
return 0;
}
}