使用JDBC预编译语句进行批量插入
将一组INSERT语句分组并一次执行它们被称为批量插入。
使用PreparedStatement对象进行批量插入
要使用PreparedStatement对象执行一批插入语句:
- 创建PreparedStatement − 使用prepareStatement()方法创建一个PreparedStatement对象。将带有占位符“?”(而不是值)的Insert查询作为参数传递给此方法。
PreparedStatement pstmt = con.prepareStatement("INSERT INTO Sales VALUES (?, ?, ?, ?, ?)");- 将值设置到占位符 − 使用setXXX()方法(setInt()、SetString()、setFloat()等)将值设置到PrepareStatement中的占位符,例如:
pstmt.setString(1, "KeyBoard"); pstmt.setString(2, "Amith"); pstmt.setString(3, "January"); pstmt.setInt(4, 1000); pstmt.setString(5, "Hyderabad");
- 将语句添加到批处理 − 将记录的值设置到占位符后,使用PreparedStatement接口的addBatch()方法将其添加到批处理,例如:
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, 500); pstmt.setString(5, "Vishakhapatnam"); pstmt.addBatch();
- 执行批处理 − 最后,使用PreparedStatement接口的executeBatch()方法执行批处理。
pstmt.executeBatch();
使用批量插入,我们可以减少通信开销并提高Java应用程序的性能。
注意 − 在将语句添加到批处理之前,需要使用con.setAutoCommit(false)关闭自动提交,并在执行批处理后使用con.commit()方法保存更改。
示例
让我们使用如下所示的CREATE语句在MySQL数据库中创建一个名为Dispatches的表:
CREATE table Dispatches ( Product_Name, varchar(255) Name_Of_Customer, varchar(255) Month_Of_Dispatch, varchar(255) Price, int(11) Location, varchar(255) );
下面的JDBC程序尝试一次性将一堆INSERT语句作为批处理执行,使用一个Statement对象。
示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class BatchInserts_PreparedStatement {
public static void main(String args[])throws Exception {
//Getting the connection
String mysqlUrl = "jdbc:mysql:///testDB";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating a Statement object
Statement stmt = con.createStatement();
//Setting auto-commit false
con.setAutoCommit(false);
PreparedStatement pstmt = con.prepareStatement("INSERT INTO Sales 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, 500);
pstmt.setString(5, "Vishakhapatnam");
pstmt.addBatch();
//Executing the batch
stmt.executeBatch();
//Saving the changes
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 | +--------------+------------------+-------------------+-------+----------------+
广告
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP