将相关的SQL语句分组为一个批处理并立即执行/提交它们称为批处理。Statement接口提供方法来执行批处理如addBatch(),executeBatch(),clearBatch()。
请按照下面给出的步骤使用Statement对象执行批更新:
使用DriverManager类的registerDriver()方法注册驱动程序类。将驱动程序类名称作为参数传递给它。
使用DriverManager类的getConnection()方法连接到数据库。将URL(字符串),用户名(字符串),密码(字符串)作为参数传递给它。
使用Connection接口的createStatement()方法创建一个Statement对象。
使用Connection接口的setAutoCommit()方法将auto-commit设置为false 。
使用Statement接口的addBatch()方法将所需的语句添加到批处理中。
使用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 | | +-------------------+--------------+------+-----+---------+-------+
以下程序使用批处理(带有语句对象)将数据插入此表中。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class BatchProcessing_Statement {
   public static void main(String args[])throws Exception {
      //获得连接
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //创建表调度(Product_Name VARCHAR(255),Name_Of_Customer-
      VARCHAR(255), Month_Of_Dispatch VARCHAR(255), Price INT, Location VARCHAR(255));
      //创建一个Statement对象
      Statement stmt = con.createStatement();
      //设置自动提交false-
      con.setAutoCommit(false);
      //插入记录的语句
      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')";
      //将语句添加到批处理中
      stmt.addBatch(insert1);
      stmt.addBatch(insert2);
      stmt.addBatch(insert3);
      //执行批处理
      stmt.executeBatch();
      //保存更改
      con.commit();
      System.out.println("Records inserted......");
   }
}Connection established...... Records inserted......
如果您验证分派表的内容,则可以观察插入的记录。
+--------------+------------------+-------------------+-------+----------------+ | 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 | +--------------+------------------+-------------------+-------+----------------+