【笔试面试考点】PreparedStatement和Statement的区别与联系&&批量插入数据的优化

目录

一.PreparedStatement和Statement的区别去联系

一.preparedStatement与Statement的联系:

二.区别:

二.批量插入数据到数据的逐步优化

一.使用Statement

二.使用PreparedStatement插入

三.使用batch缓存插入

四.改变提交的时机控制提交次数提高效率


一.PreparedStatement和Statement的区别去联系

一.preparedStatement与Statement的联系:

1.都是sun公司下的jdbc包装类中的接口,不属于外部连接;

2.reparedStatement是statement接口的子接口;

二.区别:

1.preparedStatement能最大提高系统性能,preparedStatement预编译sql语句,DBServer会对预编译的sql语句进行性能优化,预编译的sql语句在后续的批量插入时,可以被直接使用,不用再次编译,可提高读写速度

2.Statement语句中,即使操作相同的sql语句,但因为插入的内容不一样,会需要再次对sql语句进行编译,影响读写速度;

3.PreparedStatement可以防止sql注入;

4.使用PreparedStatement可以插入BLob流文件数据,而Statement无法插入Blob数据

5.使用Statement数据需要进行拼串,操作繁琐,还存在sql注入

二.批量插入数据到数据的逐步优化

一.使用Statement

二.使用PreparedStatement插入

    //批量插入方式二:使用PreparedStatement插入
    public void testInsertInfo() throws Exception {
        Connection conn = null;
        PreparedStatement pst = null;
        String sql = "insert into user_tab(id,name) values(?,?)";
        try {
            long start = System.currentTimeMillis();
            conn = JDBCUtils.getConnection();
            pst = conn.prepareStatement(sql);
            for (int i = 1; i <20000 ; i++) {
                pst.setObject(1,i);
                pst.setObject(2,"name");
                pst.execute();
            }
            long end = System.currentTimeMillis();
            System.out.println("采用preparedStatement方式插入20000条数据花费的时间为:"+(end - start));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,pst);
        }

    }

三.使用batch缓存插入

 //批量插入方式三:使用批处理插入
    /*
    1.使用 添加batch :addBatch()  执行batch:executeBatch()  清空Batch(): clearBatch();
    2.在连接数据的url后面配置这句话:
            ?rewriteBatchedStatements=true
    3.批量插入数据使用的jdbc.jar必须实在5.7版本以上才支持
     */
    @Test
    public void  testInsertInfo2()  {
        Connection conn = null;
        PreparedStatement pst = null;
        String sql = "insert into user_tab(id,name) values(?,?)";
        try{
            long start = System.currentTimeMillis();
            conn = JDBCUtils.getConnection();
            pst = conn.prepareStatement(sql);
            for (int i = 1; i <=20000 ; i++) {
                pst.setObject(1,i);
                pst.setObject(2,"name"+i);
                //1,添加batch
                pst.addBatch();
                //2.执行batch()
                if (i % 500 == 0){
                    pst.executeBatch();
                    //清空batch
                    pst.clearBatch();
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("采用batch插入20000条数据花费时间为:"+ (end - start));
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }

    }

四.改变提交的时机控制提交次数提高效率

 public void testInsertInfo3(){
        Connection conn = null;
        PreparedStatement pst = null;
        String sql = "insert into user_tab(id ,name) values(?,?)";
        try {
            long start = System.currentTimeMillis();
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            pst = conn.prepareStatement(sql);
            for (int i = 1; i <= 200000; i++) {
                pst.setObject(1,i);
                pst.setObject(2,"name"+i);
                //添加缓存
                pst.addBatch();
                if(i % 500 == 0){
                    //执行缓存
                    pst.executeBatch();
                    //清空缓存
                    pst.clearBatch();
                }
            }
            conn.commit();
            long end = System.currentTimeMillis();
            System.out.println("采用设置不允许自动提交的方式的花费的时间:"+(end - start));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,pst);
        }
    }


  • 49
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 50
    评论
评论 50
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值