批处理
业务场景:当需要向数据库发送一批SQL语句执行时,应避免向数据库一条条的发送执行
,而应采用JDBC的批处理机制,以提升执行效率。
实现批处理有两种方式,第一种方式:
Statement.addBatch(sql) list
执行批处理SQL语句
executeBatch()方法:执行批处理命令
clearBatch()方法:清除批处理命令
例:
*****************************************************************************************
user day15;
create table t3(
id int primary key,
name varchar(200)
);
public class BatchDemo {
@Test // 向t3表格中插入两条记录,并删除第一条记录 注: 批处理的语句不同,使用Statement进行批处理
public void testBatch(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
stmt = conn.createStatement();
String sql1 = "insert into t3 (id,name) values(1,'aa1')";
String sql2 = "insert into t3 (id,name) values(2,'aa2')";
String sql3 = "delete from t3 where id=1";
// 注: statement内部维护了一个List集合,将要批处理的命令加到List中即可
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
int[] ii = stmt.executeBatch(); // 元素是每条语句影响到的行数
for(int i:ii){
System.out.println(i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
@Test // 向t3中插入100条记录 注: 由于语句完全相同,只是参数不同,使用PreparedStatement
public void testBatch2(){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
String sql = "insert into t3 (id,name) values(?,?)";
stmt = conn.prepareStatement(sql);
for(int i=1;i<100;i++){
stmt.setInt(1, i);
stmt.setString(2, "sb"+i);
stmt.addBatch();
}
stmt.executeBatch();
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
@Test // 向t3中插入1000001条记录 注:mysql最多每秒存50条左右 所以最少需要5个小时左右才能存完
// 而orac只需20秒左右
public void testBatch3(){
long time = System.currentTimeMillis();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
String sql = "insert into t3 (id,name) values(?,?)";
stmt = conn.prepareStatement(sql);
for(int i=1;i<3001;i++){ // 太大了 还是换成3001条把
stmt.setInt(1, i);
stmt.setString(2, "sb"+i);
stmt.addBatch();
// 为了防止缓存List崩溃,分批存储,记得要清空缓存
if(i%1000==0){
stmt.executeBatch();
stmt.clearBatch(); // 清空缓存
}
}
stmt.executeBatch(); // 执行(最后的余数)第1000001条语句
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.release(rs, stmt, conn);
}
System.out.println("用时:"+(System.currentTimeMillis()-time)/1000+"秒");
}
}
*****************************************************************************************
业务场景:当需要向数据库发送一批SQL语句执行时,应避免向数据库一条条的发送执行
,而应采用JDBC的批处理机制,以提升执行效率。
实现批处理有两种方式,第一种方式:
Statement.addBatch(sql) list
执行批处理SQL语句
executeBatch()方法:执行批处理命令
clearBatch()方法:清除批处理命令
例:
*****************************************************************************************
user day15;
create table t3(
id int primary key,
name varchar(200)
);
public class BatchDemo {
@Test // 向t3表格中插入两条记录,并删除第一条记录 注: 批处理的语句不同,使用Statement进行批处理
public void testBatch(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
stmt = conn.createStatement();
String sql1 = "insert into t3 (id,name) values(1,'aa1')";
String sql2 = "insert into t3 (id,name) values(2,'aa2')";
String sql3 = "delete from t3 where id=1";
// 注: statement内部维护了一个List集合,将要批处理的命令加到List中即可
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
int[] ii = stmt.executeBatch(); // 元素是每条语句影响到的行数
for(int i:ii){
System.out.println(i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
@Test // 向t3中插入100条记录 注: 由于语句完全相同,只是参数不同,使用PreparedStatement
public void testBatch2(){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
String sql = "insert into t3 (id,name) values(?,?)";
stmt = conn.prepareStatement(sql);
for(int i=1;i<100;i++){
stmt.setInt(1, i);
stmt.setString(2, "sb"+i);
stmt.addBatch();
}
stmt.executeBatch();
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.release(rs, stmt, conn);
}
}
@Test // 向t3中插入1000001条记录 注:mysql最多每秒存50条左右 所以最少需要5个小时左右才能存完
// 而orac只需20秒左右
public void testBatch3(){
long time = System.currentTimeMillis();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
String sql = "insert into t3 (id,name) values(?,?)";
stmt = conn.prepareStatement(sql);
for(int i=1;i<3001;i++){ // 太大了 还是换成3001条把
stmt.setInt(1, i);
stmt.setString(2, "sb"+i);
stmt.addBatch();
// 为了防止缓存List崩溃,分批存储,记得要清空缓存
if(i%1000==0){
stmt.executeBatch();
stmt.clearBatch(); // 清空缓存
}
}
stmt.executeBatch(); // 执行(最后的余数)第1000001条语句
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.release(rs, stmt, conn);
}
System.out.println("用时:"+(System.currentTimeMillis()-time)/1000+"秒");
}
}
*****************************************************************************************