prepareStatement的批量处理数据

来源:互联网 发布:u盘数据剪切如何恢复 编辑:程序博客网 时间:2024/06/05 10:57
prepareStatement.addBatch()            //添加sql进入prepareStatement中
prepareStatement.executeBath()       //批量执行sql

//例子:
PreparedStatement ps = null;
public void addFlowCardDetail(String flowCardVouNo,List flowCardDetailList) throws DaoException {
    StringBuffer sbSql = new StringBuffer();
    sbSql.append("......");            
    try {
        Connection  conn = ConnectionManager.getConnection();
        ps = conn.prepareStatement(sbSql.toString());
        for (Iterator iter=flowCardDetailList.iterator(); iter.hasNext();) {
            ps.setString(1, flowCardVouNo);
            ......
            //ps.executeUpdate();        //不去多次与数据库打交道,采用下面的作法
            ps.addBatch();                    //PreparedStatement对象的addBatch()方法用来批量增加一组sql
        }
        ps.executeBatch();                //PreparedStatement对象的executeBatch()方法用来执行增加一组sql
    }catch(SQLException e) {
        ......
    }finally {
        ConnectionManager.close(ps);
    }        
}
0 0
原创粉丝点击