执行批量操作Performing batch operations

08/12/2019

本文内容

为了提高对 SQL ServerSQL Server 数据库进行多项更新时的性能,Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server 提供了将多项更新作为一个工作单元提交的功能,也称作“批”。To improve performance when multiple updates to a SQL ServerSQL Server database are occurring, the Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server provides the ability to submit multiple updates as a single unit of work, also referred to as a batch.

addBatch 方法可用于添加命令。The addBatch method is used to add a command. clearBatch 方法可用于清除命令列表。The clearBatch method is used to clear the list of commands. executeBatch 方法可用于提交要处理的所有命令。The executeBatch method is used to submit all commands for processing. 只有返回简单更新计数的数据定义语言 (Data Definition Language, DDL) 和数据操作语言 (Data Manipulation Language, DML) 语句可作为批处理的一部分运行。Only Data Definition Language (DDL) and Data Manipulation Language (DML) statements that return a simple update count can be run as part of a batch.

executeBatch 方法返回一个由 int 值组成的数组,这些值对应于每个命令的更新计数 。The executeBatch method returns an array of int values that correspond to the update count of each command. 如果其中一条命令失败,则会引发 BatchUpdateException,应使用 BatchUpdateException 类的 getUpdateCounts 方法检索更新计数数组。If one of the commands fails, a BatchUpdateException is thrown, and you should use the getUpdateCounts method of the BatchUpdateException class to retrieve the update count array. 如果一条命令失败,则驱动程序会继续处理剩余的命令。If a command fails, the driver continues processing the remaining commands. 但是,如果一条命令有语法错误,批处理中的语句就会失败。However, if a command has a syntax error, the statements in the batch fail.

备注

如果不是必须使用更新计数,则可以先向 SQL ServerSQL Server 发送一条 SET NOCOUNT ON 语句。If you do not have to use update counts, you can first issue a SET NOCOUNT ON statement to SQL ServerSQL Server. 这将减少网络流量并同时提高应用程序的性能。This will reduce network traffic and additionally enhance the performance of your application.

As an example, create the following table in the AdventureWorksAdventureWorks sample database:

CREATE TABLE TestTable

(Col1 int IDENTITY,

Col2 varchar(50),

Col3 int);

在下面的实例中,将向此函数传递 AdventureWorksAdventureWorks 示例数据库的打开连接,并使用 addBatch 方法创建要执行的语句,然后调用 executeBatch 方法向数据库提交批处理。In the following example, an open connection to the AdventureWorksAdventureWorks sample database is passed in to the function, the addBatch method is used to create the statements to be executed, and the executeBatch method is called to submit the batch to the database.

public static void executeBatchUpdate(Connection con) {

try {

Statement stmt = con.createStatement();

stmt.addBatch("INSERT INTO TestTable (Col2, Col3) VALUES ('X', 100)");

stmt.addBatch("INSERT INTO TestTable (Col2, Col3) VALUES ('Y', 200)");

stmt.addBatch("INSERT INTO TestTable (Col2, Col3) VALUES ('Z', 300)");

int[] updateCounts = stmt.executeBatch();

stmt.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

另请参阅See also

java 批量执行 sql_执行批量操作 - SQL Server | Microsoft Docs相关推荐

  1. java sql 查询中的转义序列不对_在 JDBC 中使用 SQL 转义序列 - SQL Server | Microsoft Docs...

    使用 SQL 转义序列Using SQL escape sequences 08/12/2019 本文内容 按照 JDBC API 的定义,Microsoft JDBC Driver for SQL ...

  2. sql server java类型_使用基本 JDBC 数据类型 - SQL Server | Microsoft Docs

    使用基本数据类型Using basic data types 01/29/2021 本文内容 Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Dr ...

  3. mysql2005备份_创建完整数据库备份 - SQL Server | Microsoft Docs

    完整数据库备份Create a Full Database Backup 09/12/2019 本文内容 适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQ ...

  4. mysql表收缩时从库也收缩么_收缩数据库 - SQL Server | Microsoft Docs

    收缩数据库Shrink a Database 03/14/2017 本文内容 适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQL ServerSQL Se ...

  5. 证书服务器无法启动,使用 SSL 证书后服务无法启动 - SQL Server | Microsoft Docs

    将实例配置为使用安全套接字层证书后,SQL Server 服务无法启动 11/03/2020 本文内容 本文提供了在配置使用 Microsoft 增强加密提供程序1.0 的 SSL 证书之后出现的问题 ...

  6. sql server服务器怎么配置文件,配置 SSAS 以生成内存转储文件 - SQL Server | Microsoft Docs...

    配置 SQL Server Analysis Services 以生成内存转储文件 09/25/2020 本文内容 本文介绍如何将 SQL Server Analysis Services 配置为自动 ...

  7. 修改服务器操作系统后sql2008出错,报告操作系统错误 1450 和 665 - SQL Server | Microsoft Docs...

    数据库文件在 DBCC CHECKDB 或数据库快照创建期间报告操作系统错误 1450 和 665 09/25/2020 本文内容 本文帮助您解决在数据库文件创建或数据库快照创建期间报告操作系统错误 ...

  8. 微软sql服务器可以禁用,禁用索引和约束 - SQL Server | Microsoft Docs

    禁用索引和约束 02/17/2017 本文内容 适用于: SQL Server(所有支持的版本) Azure SQL 数据库 本主题说明如何使用 SQL Server 或 SQL Server Man ...

  9. 文件菜单服务器属性,服务器属性(“数据库设置”页) - SQL Server | Microsoft Docs...

    服务器属性 -"数据库设置"页 05/23/2019 本文内容 适用于: SQL Server(所有支持的版本) 使用此页可以查看或修改数据库设置. 选项 默认索引填充因子 指定在 ...

最新文章

  1. Python os模块常用命令
  2. 根据Apriltag进行角度和距离检测
  3. 基于BP弱分类器用Adaboost的强分类器
  4. 一个.net的系统的AOP设计思路二——页面控件校验映射
  5. ionic 获取input的值
  6. 未能为服务器,未能为数据库 'TEMPDB' 分配新页
  7. Boost:重复计数器测试程序
  8. gradle下载的依赖包位置 及 修改
  9. java 总结几种线程异步转同步的方法
  10. docker 搜寻 包 版本_ubuntu16.04下安装docker容器
  11. Spring 3.2矩阵变量是什么? - 第1部分
  12. 程序员有哪些可以写博客的网站?
  13. shell批量修改后缀_bash批量修改文件名称的方法小结(增加,去除,修改后缀)
  14. 2007年河南湖北各市GDP
  15. 12.GitLab System Hooks
  16. 基于python的学生管理系统的设计开题报告_学生信息管理系统设计开题报告
  17. iPhone手机越狱不只是为了安装盗版应用、越狱的十大好处
  18. 基于Java Web社区医院远程挂号
  19. 【PAT】A1099. Build A Binary Search Tree (30)
  20. 如何在Total Commander中使用命令行

热门文章

  1. storm如何集成kafka
  2. python all和any用法_python any()和all()用法
  3. 如何查电脑ip地址_摄像机地址不对连不上网?教你快速更改IP地址
  4. Linux --- awk
  5. Intellij Idea系列之导Jar包与编写单元测试(二)
  6. java并发编程(更新)
  7. http://ju.outofmemory.cn/entry/307891---------TICK
  8. Spring:延迟初始化
  9. 161011、oracle批量插入数据
  10. 对于glut和freeglut的一点比较和在VS2013上的配置问题