java 批量执行 sql_执行批量操作 - SQL Server | Microsoft Docs

本文介绍了如何使用Microsoft JDBC Driver for SQL Server实现批量操作,通过addBatch、clearBatch和executeBatch方法提高对SQL Server数据库多条更新语句的执行效率。示例展示了插入数据到TestTable的批处理过程。
摘要由CSDN通过智能技术生成

执行批量操作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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值