JDBC Batch

有的时候,我们需要一次性插入很多的数据或者一次性更新、删除很多的数据,那么为了提高效率,
我们不妨使用JDBC的批处理来完成。以下就JDBC的批处理的例子展开讨论。所有应该注意的地方均
有注释。不过需要注意的是我们不能使用批处理来执行查询,即批处理语句中不可以出现select语句。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/*
 9:05:15 AM2011
 */
public class BatchExecute {

 private static Connection conn = null;
 private static PreparedStatement ps = null;
 private static Statement st=null;

 public static void main(String[] args) {
  //noStaticBatch();//非静态批
  //staticBatch();//静态批
  blendBatch();//混合模式
 }
 public static void blendBatch(){
  try{
   long start = System.currentTimeMillis();
   
   conn.setAutoCommit(false);
   st=conn.createStatement();
   st.addBatch("insert into person(name,age) values('zhuqi',22)");//插入
   st.addBatch("insert into person(name,age) values('zhaoba',21)");
   st.addBatch("update person set name = 'qita',age='100' where name = 'zhangsan'");//更新
   st.addBatch("delete from person where name like 'name%'");//删除
   st.executeBatch();
   conn.commit();
   
   System.out.print("总共用时:" + (System.currentTimeMillis() - start)
     / 1000);
  }catch(Exception e){
   
  }finally{
   closeDB();
  }
 }
 public static void staticBatch(){
  try{
   long start = System.currentTimeMillis();
   
   conn.setAutoCommit(false);
   /**以下是错误的用法,此用法中两条sql语句均为执行,
    * 如果想应用此方式的批处理,请参照blendBatch方法**/
   /*ps=conn.prepareStatement("insert into person(name,age) values('lisi',22)");
   ps=conn.prepareStatement("insert into person(name,age) values('wangwu',20)");
   ps.addBatch();*/
   
   /**以下是正确的用法**/
   String sql="insert into person(name,age) values(?,?)";
   ps=conn.prepareStatement(sql);
   ps.setString(1, "lisi");
   ps.setInt(2, 22);
   ps.addBatch();
   ps.setString(1, "wangwu");
   ps.setInt(2, 20);
   ps.addBatch();
   
   ps.addBatch("update person set name = 'zhangsan',age = 23 where name = 'lisi'");//增加静态批
   ps.executeBatch();
   conn.commit();
   
   System.out.print("总共用时:" + (System.currentTimeMillis() - start)
     / 1000);
  }catch(Exception e){
   
  }finally{
   closeDB();
  }
 }
 public static void noStaticBatch(){
  try {
   long start = System.currentTimeMillis();
   
   conn.setAutoCommit(false);// 取消自动事务提交
   String sql1 = "insert into person(name,age) values(?,?)";
   ps = conn.prepareStatement(sql1);
   for (int i = 1; i <= 110; i++) {
    ps.setString(1, "names " + i);
    ps.setInt(2, i);
    ps.addBatch();
    if (i % 20 == 0 || i == 110) {// 每20条数据执行一次、到最后必须执行一次
     ps.executeBatch();//执行批
     
     try{//这里一定要捕捉异常
      conn.commit();// 提交事务
     }catch(SQLException exc){
      conn.rollback();// 在批处理命令中,如果有一个命令出现了错误,则回滚
     }
    }
   }
   
   System.out.print("总共用时:" + (System.currentTimeMillis() - start)
     / 1000);// 测试用时
  } catch (Exception e) {
   
  }finally{//关闭数据库连接
   closeDB();
  }
 }
 static{
  try {
   Class.forName("com.mysql.jdbc.Driver");
   conn=DriverManager.getConnection("jdbc:mysql:///test","root","root");
  } catch (Exception e) {
   System.out.println("数据库连接错误");
  }
 }
 public static void closeDB(){
  if(st!=null)
   try {st.close();} catch (SQLException e1){st=null;}
  if(ps!=null)
   try {ps.close();} catch (SQLException e1){ps=null;}
  if(conn!=null)
   try {conn.close();}catch (SQLException e){conn=null;}
  System.out.println("\t 连接已关闭");
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值