JDBC批处理

JDBC批处理

  • 批量处理允许将相关的SQL语句分组到批处理中,并通过对数据库的一次调用提交它们。
  • 当需要一次向数据库发送多个SQL语句时,可以减少连接数据库的开销,从而提高性能
  • 使用批处理是需要放在事务里面,可以很有效的提高处理速度。

Statement批处理

  • (1)注册驱动获取连接
  • (2)使用createStatement()方法创建Statement对象。
  • (3)使用setAutoCommit()将auto-commit设置为false 。(可选)
  • (4)使用addBatch()方法在创建的语句对象上添加您喜欢的SQL语句到批处理中。
  • (5)在创建的语句对象上使用executeBatch()方法执行所有SQL语句。
  • (6)使用commit()方法提交所有更改。(可选)
  • (7)释放资源
  • 代码示例:
  //注册驱动
      Class.forName("com.mysql.jdbc.Driver");
      //获取连接
      //rewriteBatchedStatements=true,优化SQL语句,提高处理速度
      Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool?" + "useSSL=true&characterEncoding=utf8&rewriteBatchedStatements=true","root","root");
      //添加事务,使用这个成倍提速。设置事务为不自动提交
      connection.setAutoCommit(false);
      //创建命令
      Statement statement = connection.createStatement();
      //添加SQL语句
      for (int i = 1; i <= 10012; i++) {
          //添加插入语句
          statement.addBatch("insert into stu values('"+i+"','sdh','n',22)");
          if(i%1000==0){
              int[] ints = statement.executeBatch();
              System.out.println(ints.length);
              //清理这一批数据,重新添加一批数据
              statement.clearBatch();
          }
      }
      //最后会遗留几条数据没有添加进去,这里处理
      int[] ints = statement.executeBatch();
      System.out.println(ints.length);
      statement.clearBatch();
      //提交
      connection.commit();
      //释放资源
      statement.close();
      connection.close();

PrepareStatement批处理

  1. 使用占位符创建SQL语句。
  2. 使用prepareStatement()方法创建PrepareStatement对象。
  3. 使用setAutoCommit()将auto-commit设置为false 。(可选)
  4. 使用addBatch()方法在创建的语句对象上添加您喜欢的SQL语句到批处理中。
  5. 在创建的语句对象上使用executeBatch()方法执行所有SQL语句。
  6. 最后,使用commit()方法提交所有更改。(可选)
  • 代码示例:
    //注册驱动
         Class.forName("com.mysql.jdbc.Driver");
         //创建连接
         Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool?" + "useSSL=true&characterEncoding=utf8&rewriteBatchedStatements=true","root","root");
         //添加事务,使用这个成倍提速。设置事务为不自动提交
         connection.setAutoCommit(false);
         //创建SQL命令
         PreparedStatement preparedStatement = connection.prepareStatement("insert into stu values(?,?,?,?)");
         for (int i = 20001; i <= 30000; i++) {
             preparedStatement.setInt(1,i);
             preparedStatement.setString(2,"sdh");
             preparedStatement.setString(3,"n");
             preparedStatement.setInt(4,22);
             //上面已经添加了插入语句,这是preparedStatement预编译过了,所以这里不用重复添加插入语句
             preparedStatement.addBatch();
             if(i%1000==0){
                 int[] ints = preparedStatement.executeBatch();
                 System.out.println(ints.length);
                 //清理这一批中的数据
                 preparedStatement.clearBatch();
             }
         }
         //最后会遗留几条数据没有添加进去,这里处理
         int[] ints = preparedStatement.executeBatch();
         System.out.println(ints.length);
         preparedStatement.clearBatch();
         //提交
         connection.commit();
         //释放资源
         preparedStatement.close();
         connection.close();
    

Statment批处理和PrepareStatment批处理区别:

  • (1)Statment批处理可以添加不同Sql语句,而PrepareStatment只能添加一种sql语句
  • (2)PrepareStatment效率比Statment高,而且更安全。


转载请注明:Memory的博客 » 点击阅读原文

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦