1.JDBC批处理概述
批量处理允许您将相关的SQL语句分组到批处理中,并通过对数据库的一次调用提交它们。
当您一次向数据库发送多个SQL语句时,可以减少连接数据库的开销,从而提高性能。
JDBC实现批处理有两种方式:statement
和preparedstatement
2.使用Statement对象进行批处理操作
使用Statement对象添加要批量执行SQL语句,如下:
1.使用createStatement()方法创建Statement对象。
2.使用setAutoCommit()将auto-commit设置为false 。(将JDBC的自动提交设置为false)
3.使用addBatch()方法在创建的语句对象上添加您喜欢的SQL语句到批处理中。
4.在创建的语句对象上使用executeBatch()方法执行所有SQL语句。
5. 最后,使用commit()方法提交所有更改。
步骤一:准备数据表 employees
CREATE TABLE `employees` (
`last` varchar(20) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`first` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
步骤二:代码实现
public static void main(String[] args) throws Exception {
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/employeesdb?useUnicode=true&characterEncoding=utf-8", "root", "123");
// Create statement object
Statement stmt = conn.createStatement();
// Set auto-commit to false
conn.setAutoCommit(false);
// Create SQL statement
String SQL1 = "INSERT INTO Employees (id, first, last, age) " + "VALUES(200,'Zia', 'Ali', 30)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL1);
// Create one more SQL statement
String SQL2 = "INSERT INTO Employees (id, first, last, age) " + "VALUES(201,'Raj', 'Kumar', 35)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL2);
// Create one more SQL statement
String SQL3 = "UPDATE Employees SET age = 55 " + "WHERE id = 200";
// Add above SQL statement in the batch.
stmt.addBatch(SQL3);
// Create an int[] to hold returned values
int[] count = stmt.executeBatch();
// Explicitly commit statements to apply changes
conn.commit();
}
2.1.使用Statement对象进行批处理操作优缺点
采用Statement.addBatch(sql)方式实现批处理:
- 优点:
可以向数据库发送多条不同的SQL语句。 - 缺点:
SQL语句没有预编译。
当向数据库发送多条语句相同,但仅参数不同的SQL语句时,需重复写上很多条SQL语句。例如:
Insert into user(name,password) values('aa','111');
Insert into user(name,password) values('bb','222');
Insert into user(name,password) values('cc','333');
Insert into user(name,password) values('dd','444');
3.使用PrepareStatement对象进行批处理
使用PrepareStatement对象添加要批量执行SQL语句,如下:
- 使用占位符创建SQL语句。
- 使用prepareStatement()方法创建PrepareStatement对象。
- 使用setAutoCommit()将auto-commit设置为false 。
- 使用addBatch()方法在创建的语句对象上添加您喜欢的SQL语句到批处理中。
- 在创建的语句对象上使用executeBatch()方法执行所有SQL语句。
- 最后,使用commit()方法提交所有更改。
代码实现:
public static void main(String[] args) throws Exception {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://192.168.132.128:3306/employeesdb?useUnicode=true&characterEncoding=utf-8", "root", "123");
// Create SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " + "VALUES(?, ?, ?, ?)";
// Create PrepareStatement object
PreparedStatement 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 = pstmt.executeBatch();
// Explicitly commit statements to apply changes
conn.commit();
}
示例2:
/*
* 实例2:PrepareStatement对象进行批处理
*/
public class Demo2 {
public void test2(){
List<Employees> list=new ArrayList<Employees>();
list.add(new Employees(206, "tom1", "123", 123));
list.add(new Employees(207, "tom2", "123", 123));
list.add(new Employees(208, "tom3", "123", 123));
list.add(new Employees(209, "tom4", "123", 123));
list.add(new Employees(210, "tom5", "123", 123));
list.add(new Employees(211, "tom6", "123", 123));
list.add(new Employees(212, "tom7", "123", 123));
Connection conn=JdbcUtils.openConn();
try {
conn.setAutoCommit(false); //关闭自动提交
String sql="INSERT INTO Employees (id, first, last, age) VALUES(?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
for (Employees e : list) {
ps.setObject(1, e.getId());
ps.setObject(2, e.getFirst());
ps.setObject(3, e.getLast());
ps.setObject(4, e.getAge());
ps.addBatch();
}
int[] arrays = ps.executeBatch(); //执行批处理
conn.commit(); //手动提交
System.out.println("执行完毕:"+Arrays.toString(arrays));
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3.1.PrepareStatement对象进行批处理优点
采用PreparedStatement.addBatch()实现批处理
优点:发送的是预编译后的SQL语句,执行效率高。
缺点:只能应用在SQL语句相同,但参数不同的批处理中。因此此种形式的批处理经常用于在同一个表中批量插入数据,或批量更新表的数据。