addBatch()和executeBatch()

addBatch()

向这个PreparedStatement对象的命令批处理添加一组参数。
抛出:
SQLException—如果发生数据库访问错误,或者在关闭的PreparedStatement上调用此方法

executeBatch()

提交一批命令到数据库执行,如果所有命令都成功执行,则返回一个更新计数数组。返回的数组的int元素的顺序与批处理中的命令相对应,这些命令的顺序是根据它们添加到批处理中的顺序来排序的。executeBatch方法返回的数组元素可以是下列元素之一:
一个大于或等于零的数字——表示命令已成功处理,它是一个更新计数,给出数据库中受命令执行影响的行数
SUCCESS_NO_INFO——表示命令处理成功,但受影响的行数未知
如果批处理更新中的某个命令未能正确执行,该方法将抛出一个BatchUpdateException, JDBC驱动程序可能会继续处理批处理中的其他命令,也可能不会。然而,驱动程序的行为必须与特定的DBMS保持一致,要么总是继续处理命令,要么永远不继续处理命令。如果驱动程序在失败后继续处理,则由方法BatchUpdateException返回的数组。getUpdateCounts将包含与批处理中有多少命令一样多的元素,并且至少有一个元素如下所示:
EXECUTE_FAILED的值——表示命令未能成功执行,只有在命令失败后驱动程序继续处理命令时才会发生
可能的实现和返回值已经在Java 2 SDK,标准版,1.3版本中进行了修改,以适应在抛出一个BatchUpdateException对象后,在批量更新中继续处理命令的选项。
返回:
更新计数数组,包含批处理中的每个命令的一个元素。数组的元素按照向批处理中添加命令的顺序排列。
抛出:
SQLTimeoutException——当驱动程序确定setQueryTimeout方法指定的超时值已经超过,并且至少尝试取消当前正在运行的语句时
SQLException——如果发生数据库访问错误,这个方法会在一个封闭语句上调用,或者驱动程序不支持批处理语句。如果发送给数据库的某个命令未能正确执行或试图返回结果集,则抛出BatchUpdateException (SQLException的子类)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
addBatch is a method in the PHP PDO class that allows you to execute multiple SQL statements in a single call to the database. Here's an example: ``` // Create a PDO object and connect to the database $db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'); // Start a transaction $db->beginTransaction(); // Prepare the SQL statements $sql1 = "UPDATE users SET name = 'John' WHERE id = 1"; $sql2 = "UPDATE users SET name = 'Jane' WHERE id = 2"; // Add the SQL statements to the batch $db->prepare($sql1)->execute(); $db->prepare($sql2)->execute(); // Commit the transaction $db->commit(); ``` In this example, we first create a PDO object and connect to the database. We then start a transaction using the `beginTransaction()` method. Next, we prepare two SQL statements using the `prepare()` method and store them in the `$sql1` and `$sql2` variables. We then add these SQL statements to the batch using the `execute()` method. Finally, we commit the transaction using the `commit()` method. The `addBatch()` method is not used in this example because we are using the `execute()` method to add the SQL statements to the batch. However, you can use the `addBatch()` method to add SQL statements to the batch as well. The difference is that `execute()` immediately executes the SQL statement, while `addBatch()` adds it to the batch to be executed later. Here's an example of using `addBatch()`: ``` // Prepare the SQL statements $sql1 = "UPDATE users SET name = 'John' WHERE id = 1"; $sql2 = "UPDATE users SET name = 'Jane' WHERE id = 2"; // Create a PDO object and connect to the database $db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'); // Start a transaction $db->beginTransaction(); // Add the SQL statements to the batch $db->prepare($sql1)->addBatch(); $db->prepare($sql2)->addBatch(); // Execute the batch $db->executeBatch(); // Commit the transaction $db->commit(); ``` In this example, we first prepare the SQL statements as before. We then create a PDO object and connect to the database. Next, we start a transaction using `beginTransaction()`. Instead of using `execute()` to add the SQL statements to the batch, we use `addBatch()` to add them. After adding the SQL statements to the batch, we execute the batch using the `executeBatch()` method. Finally, we commit the transaction using the `commit()` method.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值