编写一个 JDBC 程序示例,演示如何使用 CallableStatement 对象进行批量处理?
将相关的 SQL 语句分组到批处理中并一次性执行/提交称为批处理。Statement 接口提供执行批处理的方法,例如 addBatch()、executeBatch()、clearBatch()。
按照以下步骤使用 **CallableStatement** 对象执行批处理更新
使用 DriverManager 类的 registerDriver() 方法注册驱动程序类。将驱动程序类名作为参数传递给它。
使用 **DriverManager** 类的 **getConnection()** 方法连接到数据库。将 URL(字符串)、用户名(字符串)、密码(字符串)作为参数传递给它。
使用 **Connection** 接口的 **setAutoCommit()** 方法将自动提交设置为 false。
使用 **Connection** 接口的 **prepareCall()** 方法创建一个 CallableStatement 对象。将查询(过程调用)传递给它,其中包含占位符 (?)(用于向过程传递输入参数)。
使用 **CallableStatement** 接口的 setter 方法为上述创建的语句中的占位符设置值。
使用 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 | | +-------------------+--------------+------+-----+---------+-------+
并且我们创建了一个名为 myProcedure 的过程,它将值存储在上面创建的表中,如下所示
Create procedure myProcedure ( IN Product_Name VARCHAR(255), IN Name_Of_Customer VARCHAR(255), IN Month_Of_Dispatch VARCHAR(255), IN Price INT, IN Location VARCHAR(255)) BEGIN insert into Dispatches values (); END// Query OK, 0 rows affected (0.00 sec)
以下程序调用一个名为 myProcedure 的过程,该过程将数据插入 Dispatches 表中。我们使用批处理更新为可调用语句设置值。
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
public class BatchProcessing_CallableStatement {
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));
//Setting auto-commit false
con.setAutoCommit(false);
//Creating a PreparedStatement object
CallableStatement cstmt = con.prepareCall("{call myProcedure(?, ?, ?, ?, ?)}");
cstmt.setString(1, "Keyboard");
cstmt.setString(2, "Amith");
cstmt.setString(3, "January");
cstmt.setInt(4, 1000);
cstmt.setString(5, "Hyderabad");
cstmt.addBatch();
cstmt.setString(1, "Earphones");
cstmt.setString(2, "Sumith");
cstmt.setString(3, "March");
cstmt.setInt(4, 500);
cstmt.setString(5,"Vishakhapatnam");
cstmt.addBatch();
cstmt.setString(1, "Mouse");
cstmt.setString(2, "Sudha");
cstmt.setString(3, "September");
cstmt.setInt(4, 200);
cstmt.setString(5, "Vijayawada");
cstmt.addBatch();
//Executing the batch
cstmt.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 | +--------------+------------------+-------------------+-------+----------------+
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP