JDBC批处理和事务
第一节 JDBC批处理
批量处理允许将相关的SQL语句分组到批处理中,并通过对数据库的一次调用提交它们。
当需要一次向数据库发送多个SQL语句时,可以减少连接数据库的开销,从而提高性能。
1.1 Statement批处理
以下是使用语句对象的批处理的典型步骤
- 1 注册驱动获取连接
- 2 使用*createStatement()*方法创建Statement对象。
- 3 使用*setAutoCommit()*将auto-commit设置为false 。(可选)
- 4 使用*addBatch()*方法在创建的语句对象上添加您喜欢的SQL语句到批处理中。
- 5 在创建的语句对象上使用*executeBatch()*方法执行所有SQL语句。
- 6 使用*commit()*方法提交所有更改。(可选)
- 7 释放资源
// Create statement object
Statement stmt = conn.createStatement();
// Set auto-commit to false
conn.setAutoCommit(false);
// Create SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " +
"VALUES(200,'Zia', 'Ali', 30)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL);
// Create one more SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " +
"VALUES(201,'Raj', 'Kumar', 35)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL);
// Create one more SQL statement
String SQL = "UPDATE Employees SET age = 35 " +
"WHERE id = 100";
// Add above SQL statement in the batch.
stmt.addBatch(SQL);
// Create an int[] to hold returned values
int[] count = stmt.executeBatch();
//Explicitly commit statements to apply changes
conn.commit();
1.2 PrepareStatement批处理
- 使用占位符创建SQL语句。
- 使用prepareStatement() 方法创建PrepareStatement对象。
- 使用*setAutoCommit()*将auto-commit设置为false 。(可选)
- 使用*addBatch()*方法在创建的语句对象上添加您喜欢的SQL语句到批处理中。
- 在创建的语句对象上使用*executeBatch()*方法执行所有SQL语句。
- 最后,使用*commit()*方法提交所有更改。(可选)
// Create SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " +
"VALUES(?, ?, ?, ?)";
// Create PrepareStatement object
PreparedStatemen pstmt = conn.prepareStatement(SQL);
//Set auto-commit to false
conn.setAutoCommit(false);
// Set the variables
pstmt.setInt( 1, 400 );
pstmt.setString( 2, "Pappu" );
pstmt.setString( 3, "Singh" );
pstmt.setInt( 4, 33 );
// Add it to the batch
pstmt.addBatch();
// Set the variables
pstmt.setInt( 1, 401 );
pstmt.setString( 2, "Pawan" );
pstmt.setString( 3, "Singh" );
pstmt.setInt( 4, 31 );
// Add it to the batch
pstmt.addBatch();
//add more batches
//Create an int[] to hold returned values
int[] count = stmt.executeBatch();
//Explicitly commit statements to apply changes
conn.commit();
Statment批处理和PrepareStatment批处理区别:
(1)Statment批处理可以添加不同Sql语句,而PrepareStatment只能添加一种sql语句
(2)PrepareStatment效率比Statment高,而且更安全。
第二节 JDBC操作二进制
PreparedStatement对象可以使用输入和输出流来提供参数数据。这使您可以将整个文件放入可以保存大值的数据库列,例如Text和BLOB数据类型。
有以下方法可用于流式传输数据 -
- **setAsciiStream():**此方法用于提供大的ASCII值。
- **setCharacterStream():**此方法用于提供大型UNICODE值。
- **setBinaryStream():**此方法用于提供较大的二进制值。
setXXXStream()方法除了参数占位符之外还需要额外的参数,文件大小。
考虑我们要将XML文件XML_Data.xml上传到数据库表中。这是XML文件的内容 -
<?xml version="1.0" encoding="UTF-8"?>
<Employee>
<id>100</id>
<first>张</first>
<last>无极</last>
<Salary>10000</Salary>
<Dob>18-08-1978</Dob>
</Employee>
// Import required packages
import java.sql.*;
import java.io.*;
import java.util.*;
public class JDBCExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/EMP";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[