jdbc批处理案例

来源:互联网 发布:php pdf html代码 编辑:程序博客网 时间:2024/06/05 15:03
上一篇(http://blog.csdn.net/rowandjj/article/details/8978108)我们介绍了JDBC中事务处理的知识,我们的重点是掌握事务处理的特性(ACID),以及JDBC事务处理的一般步骤,
(判断数据库是否支持事务->取消事务自动提交->添加事务信息->提交事务->异常处理,出错回滚等)。重点掌握setAutoCommit,commit,rollback等方法。
本篇我们来介绍一下JDBC中的批处理:
当需要向数据库发送一批sql语句执行时,应避免向数据库一条条的发送执行,而应该采用jdbc的批处理,可以提升执行效率.
通过addBatch()方法可以向数据库中加入多条数据,可以删除数据,同时可以执行多条记录,在向sql提交语句的时候要用executeBatch,提交之后清除缓存可以调用clearBatch方法。
看个例子:
  1. package demo;  
  2. import java.sql.Connection;  
  3. import java.sql.Date;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8. public class BatchDemo  
  9. {  
  10.     /** 
  11.      * 批处理sql语句 
  12.      * @param args 
  13.      */  
  14.     public static void main(String[] args)  
  15.     {  
  16.         long start = System.currentTimeMillis();  
  17. //        addBatchFunc();  
  18.         func();  
  19.         long end = System.currentTimeMillis();  
  20.         System.out.println(end - start);  
  21.     }  
  22.     public static void addBatchFunc()  
  23.     {  
  24.         Connection conn = null;  
  25.         PreparedStatement ps = null;  
  26.         ResultSet rs = null;  
  27.         try  
  28.         {  
  29.             conn = JDBCUtils.getConnection();  
  30.             String sql = "insert into tb_6 values (?,?,?)";  
  31.             ps = conn.prepareStatement(sql);  
  32.             for(int i = 0; i < 100 ; i++)  
  33.             {  
  34.                 ps.setString(1,"name " + i);  
  35.                 ps.setDate(2new Date(System.currentTimeMillis()));  
  36.                 ps.setFloat(3,i + 100);  
  37.                 ps.addBatch();//添加批处理语句  
  38.             }  
  39.             ps.executeBatch();//执行批处理语句  
  40.             ps.clearBatch();  
  41.         }  
  42.         catch (SQLException e)  
  43.         {  
  44.             e.printStackTrace();  
  45.         }  
  46.         finally  
  47.         {  
  48.             JDBCUtils.free(rs, ps, conn);  
  49.         }  
  50.     }  
  51.     public static void func()  
  52.     {  
  53.         Connection conn = null;  
  54.         Statement st = null;  
  55.         ResultSet rs = null;  
  56.         try  
  57.         {  
  58.             conn = JDBCUtils.getConnection();  
  59.             String sql = "insert into tb_6 values ('zh','1999.10.01','1')";  
  60.             st = conn.createStatement();  
  61.             st.addBatch(sql);  
  62.             sql = "insert into tb_6 values ('ch','1999.10.01','2')";  
  63.             st.addBatch(sql);  
  64.             st.executeBatch();  
  65.             st.clearBatch();  
  66.   
  67.         }  
  68.         catch (SQLException e)  
  69.         {  
  70.             e.printStackTrace();  
  71.         }  
  72.         finally  
  73.         {  
  74.             JDBCUtils.free(rs, st, conn);  
  75.         }  
  76.     }  
  77. }   

批处理比较简单,相信大家看了就知道了。
0 0
原创粉丝点击