编写一个JDBC程序示例,演示使用PreparedStatement对象进行批量处理?
将相关的SQL语句分组到一个批处理中,然后一次性执行/提交,这被称为批量处理。Statement接口提供了执行批量处理的方法,例如addBatch()、executeBatch()、clearBatch()。
按照以下步骤使用PreparedStatement对象执行批量更新
使用DriverManager类的**registerDriver()**方法注册驱动程序类。将驱动程序类名作为参数传递给它。
使用DriverManager类的**getConnection()**方法连接到数据库。将URL(字符串)、用户名(字符串)、密码(字符串)作为参数传递给它。
使用Connection接口的**setAutoCommit()**方法将自动提交设置为false。
使用Connection接口的**prepareStatement()**方法创建一个PreparedStatement对象。将带有占位符(?)的查询(插入)传递给它。
使用PreparedStatement接口的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 | | +-------------------+--------------+------+-----+---------+-------+
下面的程序使用批量处理(使用prepared statement对象)将数据插入到此表中。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class BatchProcessing_PreparedStatement {
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......");
//Setting auto-commit false
con.setAutoCommit(false);
//Creating a PreparedStatement object
PreparedStatement pstmt = con.prepareStatement("INSERT INTO Dispatches VALUES (?, ?, ?, ?, ?)");
pstmt.setString(1, "Keyboard");
pstmt.setString(2, "Amith");
pstmt.setString(3, "January");
pstmt.setInt(4, 1000);
pstmt.setString(5, "Hyderabad");
pstmt.addBatch();
pstmt.setString(1, "Earphones");
pstmt.setString(2, "Sumith");
pstmt.setString(3, "March");
pstmt.setInt(4, 500);
pstmt.setString(5,"Vishakhapatnam");
pstmt.addBatch();
pstmt.setString(1, "Mouse");
pstmt.setString(2, "Sudha");
pstmt.setString(3, "September");
pstmt.setInt(4, 200);
pstmt.setString(5, "Vijayawada");
pstmt.addBatch();
//Executing the batch
pstmt.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 | +--------------+------------------+-------------------+-------+----------------+
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP