`

JDBC batch批处理Statement executeBatch 详解

阅读更多

http://blog.csdn.net/basenet855x/article/details/6826731

http://san-yun.iteye.com/blog/900949

http://hi.baidu.com/zjphzxg/blog/item/7e17cf0f357f14e4aa645722.html

 

 

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的子类。

 

分享到:
评论

相关推荐

    Spring.Batch批处理框架

    Spring Batch 是一个轻量级的、完善的批处理框架,旨在帮助企业建立健壮、高效的批处理应用。Spring Batch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使得已经使用 Spring 框架的开发者或者企业更...

    Spring Batch批处理框架

    Spring Batch是一个开源的轻量级批处理框架,它提供了一整套可复用的组件,用于构建健壮且高效的批处理应用程序。由于信息给定的【部分内容】并没有提供实际的技术细节,因此我将基于Spring Batch框架本身,详细介绍...

    Spring Batch批处理详解

    2024最新!一文看懂Spring Batch批处理(大白话版,干货满满), 学习你将收获: 一.系统了解Spring Batch批处理; 二.项目中能熟练使用Spring Batch批处理

    SpringBatch批处理框架

    资源名称:Spring Batch 批处理框架内容简介:《Spring Batch 批处理框架》全面、系统地介绍了批处理框架Spring Batch,通过详尽的实战示例向读者展示了Spring Batch框架对大数据批处理的基本开发能力,并对框架的...

    JDBC高级批处理

    JDBC通过`java.sql.Statement`接口的`addBatch()`和`executeBatch()`方法实现了批处理功能。 ### 2. 使用批处理 创建批处理的基本步骤如下: 1. 创建Statement对象。 2. 使用`addBatch()`方法添加SQL语句到批处理...

    windows批处理命令使用详解[文].pdf

    Windows 批处理命令使用详解 Windows 批处理命令是指在 Windows 操作系统中,使用批处理文件(.bat 或 .cmd)来实现自动执行命令的功能。批处理命令可以简化日常或重复性任务,提高工作效率。 一、Echo 命令 Echo...

    SpringBatch批处理 刘相编

    基本篇重点讲述了数据批处理的核心概念、典型的作业配置、作业步配置,以及Spring Batch框架中经典的三步走策略:数据读、数据处理和数据写,详尽地介绍了如何对CVS格式文件、JSON格式文件、XML文件、数据库和JMS...

    spring batch批处理 教程

    Spring Batch 是一个强大的批处理框架,它为Java开发者提供了处理大量数据的能力,广泛应用于企业级应用和大数据处理中。在本文中,我们将深入探讨Spring Batch的各个方面,包括其概念、结构、执行流程以及如何在...

    Batch批处理框架.zip

    Spring Batch 是一个强大的、全面的批处理框架,用于处理大量数据。这个压缩包"Batch批处理框架.zip"包含了深入学习Spring Batch的相关资源,适合初学者到高级开发者,旨在帮助你成为批处理领域的专家。 Spring ...

    spring batch 批处理学习源码

    spring batch 批处理学习源码,原bilibili学习视频地址为 https://www.bilibili.com/video/BV1CD4y1G7Tx/?spm_id_from=333.337.search-card.all.click

    springbatch 详解PDF附加 全书源码 压缩包

    Spring Batch 是一个强大的、全面的批处理框架,由 Spring 社区开发,旨在简化企业级应用中的批量数据处理任务。这个框架提供了一种标准的方式来处理大量的数据输入和输出,使得开发者能够专注于业务逻辑,而不是...

    批处理for命令详解

    ### 批处理for命令详解 #### 一、引言 在批处理脚本编写中,`for` 命令是极为重要的一个组成部分,尤其对于初学者而言,理解和掌握 `for` 命令的使用方法是提升技能的关键。本文将深入浅出地介绍 `for` 命令的各种...

    bat批处理常用命令详解

    **批处理命令详解** 在Windows操作系统中,批处理(Batch)是一种自动化执行一系列命令的方式,主要基于DOS命令行环境。批处理脚本通常以`.bat`或`.cmd`为扩展名,它允许用户一次性执行多个命令,节省时间和提高...

    dos批处理命令详解

    ### DOS批处理命令详解 #### 一、Echo 命令 **功能**: Echo 命令用于在命令行窗口中显示消息。 **语法**: `echo [{on|off}] [message]` - `on` 或 `off`: 控制是否显示后续的命令提示符。 - `message`: 显示的消息...

    batch批处理显示汉字点阵

    汉字点阵输在生活中最常见的应用就是LED广告... 我使用c语言查找指定汉字在点阵字库中的位置并提取点阵信息,使用批处理来显示点阵文字【其中C查找点阵字库代码包含源码】 运行截图:http://pan.baidu.com/s/1o67dbEM

    bat批处理命令应用详解[参考].pdf

    bat批处理命令应用详解 本文将详细介绍bat批处理命令的应用和使用方法,包括如何创建批处理文件、批处理文件中的命令、批处理语法、call命令的使用等。 首先,批处理文件是什么?批处理文件是一种可以自动执行多个...

Global site tag (gtag.js) - Google Analytics