JDBC 批量插入

本文对比了使用JDBC的三种方式(statement、PreparedStatement、PreparedStatement+批处理)向数据库插入100000条记录的性能。实验结果显示,使用PreparedStatement+批处理的方式在Oracle10g环境中表现最佳,平均每批插入50条记录耗时约5秒,提升数据插入效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用JDBC向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试:

 

//1.使用statement插入100000条记录     
   
public void exec(Connection conn){     
    
  try {     
    
   Long beginTime = System.currentTimeMillis();     
    
   conn.setAutoCommit(false);//设置手动提交     
    
   Statement st = conn.createStatement();     
    
   for(int i=0;i<100000;i++){     
    
    String sql="insert into t1(id) values ("+i+")";     
    
    st.executeUpdate(sql);       
    
   }     
    
   Long endTime = System.currentTimeMillis();     
    
   System.out.println("st:"+(endTime-beginTime)/1000+"秒");//计算时间     
    
   st.close();     
    
   conn.close();     
    
  } catch (SQLException e) {     
    
   // TODO Auto-generated catch block     
    
   e.printStackTrace();     
    
  }       
    
 }     
    
//2.使用PreparedStatement对象     
    
public void exec2(Connection conn){     
    
  try {     
    
   Long beginTime = System.currentTimeMillis();     
    
   conn.setAutoCommit(false);//手动提交     
    
   PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");     
    
   for(int i=0;i<100000;i++){     
    
    pst.setInt(1, i);     
    
    pst.execute();         
    
   }     
    
   conn.commit();     
    
   Long endTime = System.currentTimeMillis();     
    
   System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//计算时间     
    
   pst.close();     
    
   conn.close();     
    
  } catch (SQLException e) {     
    
   // TODO Auto-generated catch block     
    
   e.printStackTrace();     
    
  }     
    
 }     
    
//3.使用PreparedStatement + 批处理     
    
public void exec3(Connection conn){     
    
  try {     
    
   conn.setAutoCommit(false);     
    
   Long beginTime = System.currentTimeMillis();     
    
   PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");   
      
   for(int i=1;i<=100000;i++){         
    
    pst.setInt(1, i);     
    
    pst.addBatch();     
    
    if(i%1000==0){//可以设置不同的大小;如50,100,500,1000等等     
    
     pst.executeBatch();     
    
     conn.commit();     
    
     pst.clearBatch();     
    
    }     
    
   }     
   // 执行不能被1000求余的SQL
   pst.executeBatch();

   conn.commit();

   pst.clearBatch();    
   Long endTime = System.currentTimeMillis();     
    
   System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒");     
    
   pst.close();     
    
   conn.close();     
    
  } catch (SQLException e) {     
    
   // TODO Auto-generated catch block     
    
   e.printStackTrace();     
    
  }     
    
 } 

 

在Oracle 10g中测试,结果:    
   
1.使用statement耗时142秒;    
   
2.使用PreparedStatement耗时56秒;    
   
3.使用PreparedStatement + 批处理耗时:    
   
   a.50条插入一次,耗时5秒;    
   
   b.100条插入一次,耗时2秒;    
   
   c.1000条以上插入一次,耗时1秒;    
   
通过以上可以得出结论,在使用jdbc大批量插入数据时,明显使用第三种方式(PreparedStatement + 批处理)性能更优。  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值