##批量提交
##使用Batch有关方法
三个方法 :
1.ps.addBatch()将一条SQL语句添加到ps中。
2.ps.executeBatch()执行Batch中所有的SQL语句。
3.ps.clearBatch()清空Batch中的SQL语句。
##使用Batch有关方法
三个方法 :
1.ps.addBatch()将一条SQL语句添加到ps中。
2.ps.executeBatch()执行Batch中所有的SQL语句。
3.ps.clearBatch()清空Batch中的SQL语句。
100条语句提交一次方法:修改一次执行一次addBatch,%100时executeBatch,然后cleatBatch,继续。
批量提交代码实现:
需求:添加一百个编过号的“大爷”到员工表,职位为“门卫”,一次提交20条语句。
public void testBatch(){
Connection con = null;
try{
con = DBUtil.getConnection(); //使用之前编写好的DBUtil线程池工具类
con.setAutoCommit(false); //在事务完成前不自动提交
String sql = "INSERT INTO emps VALUES(emps_seq.nextval,?,?,?,?,?,?,?)"; //使用sequence生成员工号
PreparedStatement ps = con.prepareStatement(sql); //解析语句
for(int i=0;i<100;i++){
ps.setString(1,"大爷"+i);
ps.setString(2,"门卫");
ps.setInt(3,1);
ps.setDate(4,new Date(System.currentTimeMillis()); //使用当前时间作为雇佣时间
ps.setDouble(5,3000.0);
ps.setDouble(6,4000.0);
ps.setInt(7, 3);
ps.addBatch(); //将当前语句添加到ps中
if(i%20==0){
ps.executeBatch(); //执行当前ps中的语句。
ps.clearBatch(); //清空语句避免重复执行。
}
}
con.commit();
}catch(SQLException e){
try{
con.rollback(); //发生异常则事务回滚 (back不大写!!!)
}catch(SQLException e1){
e1.printStackTrace();
throw new RuntimeException("事务回滚失败",e1);
}
e.printStackTrace();
throw new RuntimeException("添加员工失败",e);
}finally{
DBUtil.close(con); //用封装好的方法将连接返回线程池
}
System.out.println("添加成功");
}
#DAO
思想:先将需要封装的类封装成一个对象,
其中属性一般用包装类封装(因为包装类可以接受null参数,null在数据库中是很常见的)。
然后新建一个***DAO的接口类(***一般是表名),接口内定义了对表进行操作的常用方法。
然后新建实现类,实现接口内方法。
意义:减少了程序的耦合性,方便后续修改。也简化了常用操作。
例:EmpDaoImpl中实现EmpDao接口的findAll(查询所有员工)和findById(根据ID查找员工)方法。
代码实现:
<pre name="code" class="java"> public List<Emp> findAll(){ //返回一个装有所有员工的List
Connection con = null;
List<Emp> emps= new ArrayList<Emp>();
Emp emp = null;
try{
con = DBUtil.getConnection();
String sql = "SELECT * FROM emps";
PreparedStatement ps = con.prepareStatement(sql); //没有查询条件,所以这里也可以用Statement
ResultSet rs = ps.executeQuery();
while(rs.next()){ //迭代查询rs
emp = new Emp();
emp.setEmpno(rs.getObject("empno")); //使用getObject()是因为返回值有可能为空,getObject可以接收空参数,否则容易发生
emp.setEname(rs.getObject("ename")); //NullPointerException
emp.setJob(rs.getObject("job"));
emp.setMgr(rs.getObject("mgr"));
emp.setDate(rs.getDate("hiredate")); //getDate()可以接收空参数,故可以使用
emp.setSal(rs.getObject("sal"));
emp.setComm(rs.getObject("comm"));
emp.setDeptno(rs.getObject("deptno"));
emps.add(emp);
}
return emps;
}catch(SQLException e){
e.printStackTrace();
throw new RuntimeException("查询员工失败",e);
}finally{
DBUtil.close(con);
}
System.out.println("查询完毕");
}
public Emp findById(int id){
Connection con = null;
Emp emp = new Emp();
try{
con = DBUtil.getConnection();
String sql = "SELECT * FROM emps WHERE empno=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1,id);
ResultSet rs = ps.executeQuery();
rs.next();
emp.setEname(rs.getObject("ename")); //同上
emp.setJob(rs.getObject("job"));
emp.setMgr(rs.getObject("mgr"));
emp.setDate(rs.getDate("hiredate"));
emp.setSal(rs.getObject("sal"));
emp.setComm(rs.getObject("comm"));
emp.setDeptno(rs.getObject("deptno"));
emps.add(emp);
return emp;
}catch(SQLException e){
e.printStackTrace();
throw new RuntimeException("查询员工失败",e);
}finally{
DBUtil.close();
}
}