批量,可以大大提高众多增加、删除、变化的步伐,它是有一个非常大的数据处理效率大收益。
的“连接池”相似。事实上就是先将多次操作(增删改)打包。然后再一次发送运行
主要用到两个方法:
Ø 打包:PreparedStatement.addBatch();
Ø 发送、运行:PreparedStatement.executeBatch();
以下看做同一件事。用批处理和不用批处理的效率对照,源代码例如以下:
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author-zhipeng
*
* 对照批处理与非批处理的区别(本例的循环所在位置)
*/
public class BatchTest {
/**
* 对照“批处理”与“非批处理”的运行效率
*/
public static void main(String[] args) throws SQLException {
//非批处理,插入100条数据所花费的时间
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++)
create(i);
long end = System.currentTimeMillis();
System.out.println("create:" + (end - start));
//批处理。插入100条数据所花费的时间
start = System.currentTimeMillis();
createBatch();
end = System.currentTimeMillis();
System.out.println("createBatch:" + (end - start));
}
/**
* 非批处理-插入1条数据
*/
static void create(int i) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//JdbcUtils为自己定义的操作类,这里不多介绍
conn = JdbcUtils.getConnection();
String sql = "insert into user(name,birthday, money) values (?
, ?, ?
) "; ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, "no batch name" + i); ps.setDate(2, new Date(System.currentTimeMillis())); ps.setFloat(3, 100f + i); //运行插入 ps.executeUpdate(); } finally { //释放资源 JdbcUtils.free(rs, ps, conn); } } /** * 批处理-插入100条数据 */ static void createBatch() throws SQLException { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JdbcUtils.getConnection(); String sql = "insert into user(name,birthday, money) values (?
, ?
, ?) "; ps = conn.prepareStatement(sql); //注意批处理与“非批处理”循环放的位置 for (int i = 0; i < 100; i++) { ps.setString(1, "batch name" + i); ps.setDate(2, new Date(System.currentTimeMillis())); ps.setFloat(3, 100f + i); //关键方法1:打包 ps.addBatch(); } //关键方法2:运行 int[] is = ps.executeBatch(); } finally { JdbcUtils.free(rs, ps, conn); } } }
执行效果:
这是运行100条数据的差距,能够想象对大数据的效率提升改有多大。
版权声明:本文博客原创文章。博客,未经同意,不得转载。