编写一个 JDBC 程序示例,演示使用 Statement 对象进行批处理?


将相关的 SQL 语句分组到一个批处理中,并一次性执行/提交,这被称为批处理。Statement 接口提供了执行批处理的方法,例如 addBatch()、executeBatch()、clearBatch()。

请按照以下步骤使用 Statement 对象执行批更新

  • 使用 **DriverManager** 类的 **registerDriver()** 方法注册驱动程序类。将驱动程序类名作为参数传递给它。

  • 使用 **DriverManager** 类的 **getConnection()** 方法连接到数据库。将 URL(字符串)、用户名(字符串)、密码(字符串)作为参数传递给它。

  • 使用 **Connection** 接口的 **createStatement()** 方法创建 Statement 对象。

  • 使用 **Connection** 接口的 **setAutoCommit()** 方法将自动提交设置为 false。

  • 使用 **Statement** 接口的 **addBatch()** 方法将所需语句添加到批处理中。

  • 使用 **Statement** 接口的 **executeBatch()** 方法执行批处理。

  • 使用 **Statement** 接口的 **commit()** 方法提交所做的更改。

示例

假设我们创建了一个名为 **Dispatches** 的表,其描述如下

+-------------------+--------------+------+-----+---------+-------+
| Field             | Type         | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| Product_Name      | varchar(255) | YES  |     | NULL    |       |
| Name_Of_Customer  | varchar(255) | YES  |     | NULL    |       |
| Month_Of_Dispatch | varchar(255) | YES  |     | NULL    |       |
| Price             | int(11)      | YES  |     | NULL    |       |
| Location          | varchar(255) | YES  |     | NULL    |       |
+-------------------+--------------+------+-----+---------+-------+

以下程序使用批处理(使用 statement 对象)将数据插入此表。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class BatchProcessing_Statement {
   public static void main(String args[])throws Exception {
      //Getting the connection
      String mysqlUrl = "jdbc:mysql:///sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //CREATE TABLE Dispatches( Product_Name VARCHAR(255), Name_Of_Customer
      VARCHAR(255), Month_Of_Dispatch VARCHAR(255), Price INT, Location VARCHAR(255));
      //Creating a Statement object
      Statement stmt = con.createStatement();
      //Setting auto-commit false
      con.setAutoCommit(false);

      //Statements to insert records
      String insert1 = "INSERT INTO Dispatches( Product_Name , Name_Of_Customer , "
         + "Month_Of_Dispatch , Price, Location) VALUES "
         + "('KeyBoard', 'Amith', 'January', 1000, 'hyderabad')";
      String insert2 = "INSERT INTO Dispatches( Product_Name , Name_Of_Customer , "
         + "Month_Of_Dispatch , Price, Location) VALUES "
         + "('Earphones', 'SUMITH', 'March', 500, 'Vishakhapatnam')";
      String insert3 = "INSERT INTO Dispatches( Product_Name , Name_Of_Customer , "
         + "Month_Of_Dispatch , Price, Location) VALUES "
         + "('Mouse', 'Sudha', 'September', 200, 'Vijayawada')";
      //Adding the statements to the batch
      stmt.addBatch(insert1);
      stmt.addBatch(insert2);
      stmt.addBatch(insert3);
      //Executing the batch
      stmt.executeBatch();
      //Saving the changes
      con.commit();
      System.out.println("Records inserted......");
   }
}

输出

Connection established......
Records inserted......

如果验证 Dispatches 表的内容,您可以观察插入的记录。

+--------------+------------------+-------------------+-------+----------------+
| Product_Name | Name_Of_Customer | Month_Of_Dispatch | Price | Location       |
+--------------+------------------+-------------------+-------+----------------+
| KeyBoard     | Amith            | January           | 1000  | hyderabad      |
| Earphones    | SUMITH           | March             | 500   | Vishakhapatnam |
| Mouse        | Sudha            | September         | 200   | Vijayawada     |
+--------------+------------------+-------------------+-------+----------------+

更新于: 2019-07-30

129 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.