package dao;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import entity.Emp;
import util.DBUtil;
public class EmpDao implements Serializable {
public List<Emp> findByPage(int page,int size){
Connection conn =null;
try {
conn = DBUtil.getConnection();
String sql = "select * from(select t.*,rownum r from(select * from emps_brown order by empno)t)where r between ? and ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, (page-1)*size+1);
ps.setInt(2, page*size);
ResultSet rs = ps.executeQuery();
List<Emp> list = new ArrayList<Emp>();
while(rs.next()){
//每次循环所获得的数据封装到Emp对象中给调用者返回。
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"));
list.add(e);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("分页查询员工失败",e);
} finally{
DBUtil.close(conn);
}
}
/**
* 根据ID最多能查出一条数据,
* 所以只需要遍历一次即可。
* select * from emps where empno=?
* @param id
* @return
*/
public Emp findById(int id){
return null;
}
//增(DML)
public void save(Emp emp){
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "insert into emps_brown values(emps_seq_brown.nextval,?,?,?,?,?,?,?)";
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 (SQLException e) {
e.printStackTrace();
throw new RuntimeException("增加员工失败",e);
} finally{
DBUtil.close(conn);
}
}
/**
* update emps set
* ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=?
* where empno=?
* @param emp
*/
//改
public void update(Emp emp){
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql2 = "update emps_brown set ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=?where empno=?";
PreparedStatement ps2 = conn.prepareStatement(sql2);
ps2.setString(1, emp.getEname());
ps2.setString(2, emp.getJob());
ps2.setInt(3, emp.getMgr());
ps2.setDate(4, emp.getHiredate());
ps2.setDouble(5, emp.getSal());
ps2.setDouble(6, emp.getComm());
ps2.setInt(7, emp.getDeptno());
ps2.setInt(8, emp.getEmpno());
ps2.executeUpdate();
} catch (SQLException e1) {
e1.printStackTrace();
throw new RuntimeException("更新员工失败",e1);
}finally{
DBUtil.close(conn);
}
}
/**
* delete from emps where empno=?
* @param id
*/
//删
public void delete(int id){
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql3 = "delete from emps_brown where empno=?";
PreparedStatement ps3 = conn.prepareStatement(sql3);
ps3.setInt(1, id);
ps3.executeUpdate();
} catch (SQLException e2) {
e2.printStackTrace();
throw new RuntimeException(e2);
}
}
}
package test;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.junit.Test;
import dao.EmpDao;
import entity.Emp;
import util.DBUtil;
public class TestDay03 {
/**
* 测试EmpDao.delete()
*/
@Test
public void test7(){
Emp e = new Emp();
EmpDao dao = new EmpDao();
dao.delete(9);
}
/**
* 测试EmpDao.save()
*/
@Test
public void test6(){
//假设页面传入的数据如下
String ename = "八戒";
String job = "保镖";
int mgr = 0;
Date hiredate = new Date(System.currentTimeMillis());
double sal = 7000.0;
double comm = 2000.0;
int deptno = 2;
int empno = 12;
//封装这些数据,然后保存
Emp e = new Emp();
e.setEname(ename);
e.setJob(job);
e.setMgr(mgr);
e.setHiredate(hiredate);
e.setSal(sal);
e.setComm(comm);
e.setDeptno(deptno);
e.setEmpno(empno);
EmpDao dao = new EmpDao();
dao.update(e);
}
/**
* 测试EmpDao.save()
*/
@Test
public void test5(){
//假设页面传入的数据如下
String ename = "八戒";
String job = "保镖";
int mgr = 0;
Date hiredate = new Date(System.currentTimeMillis());
double sal = 7000.0;
double comm = 2000.0;
int deptno = 2;
//封装这些数据,然后保存
Emp e = new Emp();
e.setEname(ename);
e.setJob(job);
e.setMgr(mgr);
e.setHiredate(hiredate);
e.setSal(sal);
e.setComm(comm);
e.setDeptno(deptno);
EmpDao dao = new EmpDao();
dao.save(e);
}
/**
* 测试EmpDao.findBypage()
*/
@Test
public void test4(){
EmpDao dao = new EmpDao();
List<Emp> list = dao.findByPage(3,10);
for(Emp e:list){
System.out.println(e.getEmpno()+","+e.getEname());
}
}
/**
* 演示如何做真分页查询
*/
@Test
public void test3(){
//假设需求规定了每页显示的行数
int size = 10;
//假设用户点击了第2页
int page = 2;
Connection conn = null;
try {
conn = DBUtil.getConnection();
String sql = "select * from(select t.*,rownum r from(select * from emps_brown order by empno)t)where r between ? and ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, (page-1)*size+1);//start
ps.setInt(2, page*size);//end
ResultSet rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getInt("empno"));
System.out.println(rs.getString("ename"));
System.out.println("-------------------");
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("分页查询员工失败",e);
} finally{
DBUtil.close(conn);
}
}
/**
* 演示如何使用ps记录刚刚生成的ID。
* 场景:增加部门,然后立刻给此部门增加一个员工
*/
@Test
public void test2(){
//假设页面传入的要增加的数据是
//部门
String dname = "人事部";
String loc = "北京";
//员工
String ename = "苍老师";
String job="招人";
int mgr = 0;
Date hiredate = new Date(System.currentTimeMillis());
double sal = 8000.0;
double comm = 3000.0;
Connection conn = null;
try {
conn = DBUtil.getConnection();
conn.setAutoCommit(false);//取消自动提交
//1.插入部门
String sql = "insert into depts_brown values (depts_seq_brown.nextval,?,?)";
//prepareStatement()是重载的方法
//第2个参数是一个数组,存需要ps帮忙记录的字段的名字。
PreparedStatement ps = conn.prepareStatement(sql,new String[]{"deptno"});
ps.setString(1, dname);
ps.setString(2, loc);
ps.executeUpdate();//执行DML语句
//2.从ps中获取它记录的数据
//返回的结果集中存了需要它记录的数据,由ResultSet接口接收。
//获取这些数据必须通过字段序号获取
ResultSet rs = ps.getGeneratedKeys();
rs.next();
int deptno = rs.getInt(1);//获取序号,第一个字段的值
System.out.println(deptno);
//3.插入员工
String sql2 = "insert into emps_brown values(emps_seq_brown.nextval,?,?,?,?,?,?,?)";
PreparedStatement ps2 = conn.prepareStatement(sql2);
ps2.setString(1, ename);
ps2.setString(2, job);
ps2.setInt(3, mgr);
ps2.setDate(4, hiredate);
ps2.setDouble(5, sal);
ps2.setDouble(6, comm);
ps2.setInt(7, deptno);
ps2.executeUpdate();
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
throw new RuntimeException("回滚失败",e1);
}
e.printStackTrace();
throw new RuntimeException("插入部门和员工失败",e);
} finally{
DBUtil.close(conn);
}
}
/**
* 演示如何使用ps批量插入108个员工
* 重点是如下三个方法的使用:
* addBatch() executeBatch() clearBatch()
*/
@Test
public void test1(){
//这是一项业务,要保证在一个事物内
Connection conn = null;
try {
conn = DBUtil.getConnection();
conn.setAutoCommit(false);//取消了自动提交
//批量插入的员工的SQL是一样的
//所以只需要发送数据库一次
String sql = "insert into emps_brown values (emps_seq_brown.nextval,?,?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);//发送
for(int i=1;i<=108;i++){
//每次循环将员工存入ps
//每循环50次,将这些数据批量发送给数据库。
ps.setString(1,"好汉"+i);
ps.setString(2, "打劫");
ps.setInt(3, 0);
ps.setDate(4, new Date(System.currentTimeMillis()));
ps.setDouble(5, Math.random()*10000);
ps.setDouble(6, Math.random()*10000);
ps.setInt(7,5);
ps.addBatch();//把当前的数据存入ps里
if(i%50==0){
ps.executeBatch();//PreparedStatement对象语句列表中的所有SQL语句发送给数据库进行处理
ps.clearBatch();//清空当前SQL语句列表
}
}
//为了避免存在零头(8),单独发送一次。
//因为这是最后一次发送,就不用清空了
ps.executeBatch();
conn.commit();//手动提交事物
} catch (SQLException e) {
try {
conn.rollback();//如果提交事物失败,要回滚事物
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
throw new RuntimeException("回滚失败",e1);
}
e.printStackTrace();
throw new RuntimeException("批量插入员工失败",e);
} finally{
DBUtil.close(conn);
}
}
}