zoukankan      html  css  js  c++  java
  • jdbc批处理

    在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率。

    JDBC实现批处理有两种方式:statement和preparedstatement

    一、使用Statement完成批处理

    1、使用Statement对象添加要批量执行SQL语句,如下:

    1 Statement.addBatch(sql1);
    2 Statement.addBatch(sql2);
    3 Statement.addBatch(sql3);
    

    2、执行批处理SQL语句:Statement.executeBatch();
    3、清除批处理命令:Statement.clearBatch();

    1.1、使用Statement完成批处理范例

    1、编写测试的SQL脚本创建表

    1  create table testbatch
    2 (
    3      id int primary key,
    4      name varchar(20)
    5 );

    2、编写测试代码,如下所示:

    /**
    * @ClassName: JdbcBatchHandleByStatement
    * @Description: 使用prepareStatement实现JDBC批处理操作
    * @author: 孤傲苍狼
    * @date: 2014-9-20 下午10:05:45
    *
    */ 
    public class JdbcBatchHandleByPrepareStatement {
    
        @Test
        public void testJdbcBatchHandleByPrepareStatement(){
            long starttime = System.currentTimeMillis();
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            
            try{
                conn = JdbcUtils.getConnection();
                String sql = "insert into testbatch(id,name) values(?,?)";
                st = conn.prepareStatement(sql);
                for(int i=1;i<1000008;i++){  //i=1000  2000
                    st.setInt(1, i);
                    st.setString(2, "aa" + i);
                    st.addBatch();
                    if(i%1000==0){
                        st.executeBatch();
                        st.clearBatch();
                    }
                }
                st.executeBatch();
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtils.release(conn, st, rs);
            }
            long endtime = System.currentTimeMillis();
            System.out.println("程序花费时间:" + (endtime-starttime)/1000 + "秒!!");
        }
    }

    1.2、采用Statement.addBatch(sql)方式实现批处理的优缺点

    采用Statement.addBatch(sql)方式实现批处理:
        优点:可以向数据库发送多条不同的SQL语句。
        缺点:SQL语句没有预编译。
        当向数据库发送多条语句相同,但仅参数不同的SQL语句时,需重复写上很多条SQL语句。例如:

    1 Insert into user(name,password) values('aa','111');
    2 Insert into user(name,password) values('bb','222');
    3 Insert into user(name,password) values('cc','333');
    4 Insert into user(name,password) values('dd','444');

    二、使用PreparedStatement完成批处理

    2.1、使用PreparedStatement完成批处理范例
      测试代码如下:

    /**
    * @ClassName: JdbcBatchHandleByStatement
    * @Description: 使用prepareStatement实现JDBC批处理操作
    * @author: 孤傲苍狼
    * @date: 2014-9-20 下午10:05:45
    *
    */ 
    public class JdbcBatchHandleByPrepareStatement {
    
        @Test
        public void testJdbcBatchHandleByPrepareStatement(){
            long starttime = System.currentTimeMillis();
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            
            try{
                conn = JdbcUtils.getConnection();
                String sql = "insert into testbatch(id,name) values(?,?)";
                st = conn.prepareStatement(sql);
                for(int i=1;i<1000008;i++){  //i=1000  2000
                    st.setInt(1, i);
                    st.setString(2, "aa" + i);
                    st.addBatch();
                    if(i%1000==0){
                        st.executeBatch();
                        st.clearBatch();
                    }
                }
                st.executeBatch();
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtils.release(conn, st, rs);
            }
            long endtime = System.currentTimeMillis();
            System.out.println("程序花费时间:" + (endtime-starttime)/1000 + "秒!!");
        }
    }

    2.2、采用PreparedStatement.addBatch()方式实现批处理的优缺点

    采用PreparedStatement.addBatch()实现批处理
        优点:发送的是预编译后的SQL语句,执行效率高。
        缺点:只能应用在SQL语句相同,但参数不同的批处理中。因此此种形式的批处理经常用于在同一个表中批量插入数据,或批量更新表的数据。

    心得:

    锻炼自己钢铁般的意志,挑战“不可能”打垮“做不到”,越是艰难险阻,越是勇往直前。
    以自己勇敢的精神在任何艰难险阻面前都不退缩。

    克服恐惧,是我们走向成功的第一步。做到全力以赴,就可以再奋斗中成长,在风险中获得意外的收获。
  • 相关阅读:
    org.hibernate.annotationexception no identifier specified for entity
    PL/SQL Developer 中文乱码解决
    cron表达式
    mysql远程连接的设置
    linux查看端口对应的程序及pid
    安卓开发分享功能,分享到facebook网页上不显示图片的问题
    win7下解压安装mysql的方法
    总结一下论文写作过程中的一些东西
    java中可以让程序暂停几秒执行的代码
    Neo4j图数据库使用
  • 原文地址:https://www.cnblogs.com/www-tty-com/p/13634722.html
Copyright © 2011-2022 走看看