QueryRunner批量执行语句方法

问题:当库中有多条的数据需要修改等操作时,一条一条去update,必定不是一个好方法.

解决:executeBatch();批量执行

代码:

public String update() throws Exception{
        QueryRunner qr = dbUtil.getQueryRunner();
        Connection con = qr.getDataSource().getConnection();//获取连接对象
        con.setAutoCommit(false);//设置禁用自动提交
        Statement statm = con.createStatement();//创建执行对象
        //这里可以通过addBatch()方法增加多条任意SQL语句
        statm.addBatch("insert into table(moduletype) values('aaa')");
        statm.addBatch("insert into table(moduletype) values('bbb')");
        //执行
        statm.executeBatch();
        con.commit();//提交
        con.setAutoCommit(true);//开启自动提交
        //关流
        statm.close();
        con.close();
        return "完成";
    }

猜你喜欢

转载自blog.csdn.net/weixin_40648180/article/details/81939628