JDBC提供了数据库batch处理的能力,在数据大批量操作(新增、删除等)的情况下能够大幅度提升系统的性能。我曾经接触的一个项目,在没有採用batch处理时,删除5万条数据大概要半个小时左右,后来对系统进行改造,採用了batch处理的方式,删除5万条数据基本上不会超过1分钟。看一段JDBC代码:

// 关闭自己主动运行

con.setAutoCommit(false);

Statement stmt = con.createStatement();



stmt.addBatch("INSERT INTO employees VALUES (1000, 'Joe Jones')");

stmt.addBatch("INSERT INTO departments VALUES (260, 'Shoe')");

stmt.addBatch("INSERT INTO emp_dept VALUES (1000, 260)");



// 提交一批要运行的更新命令

int[] updateCounts = stmt.executeBatch();

本例中禁用了自己主动运行模式,从而在调用 Statement.executeBatch() 时可以防止 JDBC 运行事务处理。禁用自己主动运行使得应用程序可以在错误发生及批处理中的某些命令不能运行时决定是否运行事务处理。因此,当进行批处理更新时,通常应该关闭自己主动运行。

在JDBC 2.0 中,Statement 对象可以记住可以一起提交运行的命令列表。创建语句时,与它关联的命令列表为空。Statement.addBatch() 方法为调用语句的命令列表加入�一个元素。假设批处理中包括有试图返回结果集的命令,则当调用 Statement. executeBatch() 时,将抛出 SQLException。仅仅有 DDL 和 DML 命令(它们仅仅返回简单的更新计数)才干作为批处理的一部分来运行。假设应用程序决定不提交已经为某语句构

造的命令批处理,则能够调用方法 Statement.clearBatch()(以上没有显示)来又一次设置批处理。

Statement.executeBatch() 方法将把命令批处理提交给基本 DBMS 来运行。命令的运行将按照在批处理中的加入�顺序来进行。ExecuteBatch() 为运行的命令返回更新计数数组。数组中相应于批处理中的每一个命令都包括了一项,而数组中各元素根据命令的运行顺序(这还是和命令的最初加入�顺序同样)来排序。调用executeBatch() 将关闭发出调用的 Statement 对象的当前结果集(假设有一个结果集是打开的)。executeBatch() 返回后,将又一次将语句的内部批处理命令列表设置为空。

假设批处理中的某个命令无法正确运行,则 ExecuteBatch() 将抛出BatchUpdateException。能够调用BatchUpdateException.getUpdateCounts() 方法来为批处理中成功运行的命令返回更新计数的整型数组。由于当有第一个命令返回错误时,Statement.executeBatch() 就中止,并且这些命令是根据它们在批处理中的加入�顺序而运行的。所以假设 BatchUpdateException.getUpdateCounts() 所返回的数组包括
N 个元素,这就意味着在调用 executeBatch() 时批处理中的前 N 个命令被成功运行。用PreparedStatement 能够象以下这样写代码:

// 关闭自己主动运行

con.setAutoCommit(false);

PreparedStatement stmt = con.prepareStatement("INSERT INTO employees VALUES (?, ?)");




stmt.setInt(1, 2000);

stmt.setString(2, "Kelly Kaufmann");

stmt.addBatch();

// 提交要运行的批处理

int[] updateCounts = stmt.executeBatch();

========================================

PrepareStatement 也是接口

PrepareStatement extends Statement

PrepareStatement 本身没有 int[] executeBatch() throws SQLException 方法

而是继承了Statement的方法,且它们都是接口没有实际实现方法,但Statement

接口对executeBatch()方法做了规范

/**

     * Submits a batch of commands to the database for execution and

     * if all commands execute successfully, returns an array of update counts.

       每次提交一批命令到数据库中运行,假设全部的命令都成功运行了,那么返回一个

       数组,这个数组是说明每条命令所影响的行数

     * The <code>int</code> elements of the array that is returned are ordered

     * to correspond to the commands in the batch, which are ordered

     * according to the order in which they were added to the batch.

       返回的数组中每一个整型值都是排过序的,它们的顺序和批量处理中的命令们是一致的,

       命令的顺序是依照它们被加到批处理中的顺序一致。

     * The elements in the array returned by the method <code>executeBatch</code>

     * may be one of the following:

       executeBatch方法返回的数组中的元素可能是以下几种情形之中的一个:

     * <OL>

     * <LI>A number greater than or equal to zero -- indicates that the

     * command was processed successfully and is an update count giving the

     * number of rows in the database that were affected by the command's

     * execution

       一个大于或等于零的数字,简单说来命令成功运行后就返回它所影响到的行的数目

     * <LI>A value of <code>SUCCESS_NO_INFO</code> -- indicates that the command was

     * processed successfully but that the number of rows affected is

     * unknown

       

      * The constant indicating that a batch statement executed successfully

      * but that no count of the number of rows it affected is available.

      int SUCCESS_NO_INFO = -2;

      常量SUCCESS_NO_INFO代表的值=-2,也就是说命令运行成功了但命令影响到的行数

      无法统计,是未知的,仅仅能返回SUCCESS_NO_INFO来说明命令运行情况。

     * <P> * If one of the commands in a batch update fails to execute properly,

     * this method throws a <code>BatchUpdateException</code>, and a JDBC

     * driver may or may not continue to process the remaining commands in

     * the batch.

       假设批量处理时当中一个命令运行失败,则会抛出一个异常BatchUpdateException

       JDBC驱动可能会停止剩余的命令,也可能继续运行剩余的命令。

     * However, the driver's behavior must be consistent with a

     * particular DBMS, either always continuing to process commands or never

     * continuing to process commands.

       无论如何,驱动要怎么做取决于数据库管理系统的细节,总是运行或总是不运行两者其一。

     * If the driver continues processing

     * after a failure, the array returned by the method

     * <code>BatchUpdateException.getUpdateCounts</code>

     * will contain as many elements as there are commands in the batch, and

     * at least one of the elements will be the following:

       发生失败后假设驱动继续运行,通过BatchUpdateException.getUpdateCounts()方法返回

       的数组应该包含批处理中有的那些命令的结果,而且至少有一个元素的值是以下的情况:

     * <P>

     * <LI>A value of <code>EXECUTE_FAILED</code> -- indicates that the command failed

     * to execute successfully and occurs only if a driver continues to

     * process commands after a command fails

           int EXECUTE_FAILED = -3;

           指示命令没有成功运行的常量值EXECUTE_FAILED,而且仅仅有在命令出错后驱动继续运行的情况下才会出现,

           假设出错后不再运行,则返回的结果中没有错误信息仅仅有那些被成功运行后的结果。

     * </OL>

     * <P> * A driver is not required to implement this method.

     * The possible implementations and return values have been modified in

     * the Java 2 SDK, Standard Edition, version 1.3 to

     * accommodate the option of continuing to proccess commands in a batch

     * update after a <code>BatchUpdateException</code> obejct has been thrown.

       驱动不实现此方法,可能会出现的实现和返回值在Java 2 SDK,Standard Edition,

       version 1.3 ,以适应批处理时抛出BatchUpdateException 异常后是继续运行还是

       终止运行的选项。

      

     * @return an array of update counts containing one element for each

     * command in the batch. The elements of the array are ordered according

     * to the order in which commands were added to the batch.

       返回一个和加入�命令时的顺序一样的数组结果

     * @exception SQLException if a database access error occurs or the

     * driver does not support batch statements. Throws {@link BatchUpdateException}

     * (a subclass of <code>SQLException</code>) if one of the commands sent to the

     * database fails to execute properly or attempts to return a result set.

     * @since 1.3

     */

       假设数据库訪问异常或驱动不支持批处理命令,或者假设一个命令发送到数据库时失败或尝试取得结果集

       时失败,都会抛一个异常BatchUpdateException 它是SQLException的子类。

JDBC batch批处理Statement executeBatch 具体解释的更多相关文章

  1. JDBC batch批量Statement executeBatch 详细解释

    JDBC提供了数据库batch处理的能力,在数据大批量操作(新增.删除等)的情况下能够大幅度提升系统的性能.我曾经接触的一个项目,在没有採用batch处理时,删除5万条数据大概要半个小时左右,后来对系 ...

  2. JDBC进行批处理Batch

    在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...

  3. [javaSE] JDBC的批处理

    向数据库发送多条sql语句 create database batch use batch create table batch_table( id int primary key auto_incr ...

  4. JDBC的批处理操作三种方式 pstmt.addBatch()

    package lavasoft.jdbctest; import lavasoft.common.DBToolkit; import java.sql.Connection; import java ...

  5. JDBC进行批处理

    转自 http://mousepc.iteye.com/blog/1131462 业务场景:当需要向数据库发送一批SQL语句执行时,应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升 ...

  6. 使用JDBC进行批处理

    在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...

  7. JavaWeb学习总结(十一)--JDBC之批处理

    一.批处理的介绍 在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率.批处理只针对更新(增.删.改)语句,批 ...

  8. javaweb学习总结(三十六)——使用JDBC进行批处理

    在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...

  9. JDBC的批处理操作三种方式

    SQL批处理是JDBC性能优化的重要武器,批处理的用法有三种. package lavasoft.jdbctest; import lavasoft.common.DBToolkit; import ...

随机推荐

  1. Cloud Foundry中 JasperReports service集成

    Cloud Foundry作为业界第一个开源的PaaS解决方案,正越来越多的被业界接受和认可.随着PaaS的发展,Cloud Foundry顺应潮流,充分发挥开源项目的特点,到目前为止,已经支持了大批 ...

  2. iOS开发,新手入门指导

    在做了近两年wp,安卓开发之后,某一天突然决定投身iOS的开发之中. 因为一直用的mac,做wp开发的时候都用双系统,vs开久了,就会比较烫,这点让人不爽.后来更多地做安卓,直接mac下开发,很舒适的 ...

  3. 理解线程的挂起,sleep还有阻塞

    线程是靠cpu来运行的,cpu要运行一个线程(不说别的)最起码就是要占用cpu时间,象Windows这样的多任务操作系统,可以允许多个线程同时运行,所谓的同时运行并不是真正的同时运行,而是轮流运行不同 ...

  4. 初涉JavaScript模式 (5) : 原型模式 【一】

    什么是原型模式? 原型模式(prototype)是指用原型实例指向创建对象的种类,并且通过拷贝这些原型创建新的对象.--引自JavaScript设计模式 我们创建的每一个函数都有一个prototype ...

  5. iOS常见内存泄漏解决

    iOS常见内存泄漏解决     1 OC和CF转化出现的内存警告 CFStringRef cfString = CFURLCreateStringByAddingPercentEscapes(kCFA ...

  6. Ant快速入门(一)-----Ant介绍

    Ant是一种基于Java的生成工具.从作用上来看,它类似于C编程(UNIX平台上使用比较多)的Make工具,C/C++项目经常使用Make工具来管理整个项目的编译,生成 Make使用Shell命令来定 ...

  7. sql server 2008 case when

    CASE WHEN的两种格式 1.简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END 2.Case搜索函数 CASE ...

  8. DC-DC转换器原理与应用

    DC/DC转换器为转变输入电压后,有效输出固定电压的电压转换器.DC/DC转换器分为三类:升压型DC/DC转换器.降压型DC/DC转换器以及升降压型DC/DC转换器.根据需求可采用三类控制.PWM控制 ...

  9. redis 扩展安装使用

    Redis的php客户端库非常之多, Redis推荐客户端链接是:http://redis.io/clients 推荐用phpredis,下载地址:https://github.com/nicolas ...

  10. POJSorting It All Out (拓扑)

    题目链接. 题目大意: 给定一定的数量的小于关系: 1.如果发现环,输出从前几次就可以确定出现环 2.如果能够确定唯一序列,输出. 3.如果通过全部关系,还不能确定序列,则输出不能确定. 分析: 个人 ...