[Written test interview test site] The difference and connection between PreparedStatement and Statement && optimization of batch inserting data

content

1. The difference between PreparedStatement and Statement to contact

1. The connection between preparedStatement and Statement:

2. Difference:

2. Gradual optimization of batch inserting data into data

1. Use Statement

2. Insert using PreparedStatement

3. Use batch cache to insert

Four. Change the timing of submission to control the number of submissions to improve efficiency


1. The difference between PreparedStatement and Statement to contact

1. The connection between preparedStatement and Statement:

1. They are all interfaces in the jdbc packaging class under sun company, and do not belong to external connections;

2.reparedStatement is a sub-interface of the statement interface;

2. Difference:

1. PreparedStatement can maximize system performance. PreparedStatement precompiles SQL statements, and DBServer will optimize the performance of the precompiled SQL statements. The precompiled SQL statements can be used directly in subsequent batch inserts without recompiling, which can improve the performance. read and write speed

2. In the Statement statement, even if the same SQL statement is operated, because the inserted content is different, the SQL statement will need to be compiled again, which will affect the reading and writing speed;

3. PreparedStatement can prevent sql injection;

4. Using PreparedStatement can insert BLob stream file data, but Statement cannot insert Blob data

5. The use of Statement data requires stringing, the operation is cumbersome, and there is also sql injection

2. Gradual optimization of batch inserting data into data

1. Use Statement

2. Insert using 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);
        }

    }

3. Use batch cache to insert

 //批量插入方式三:使用批处理插入
    /*
    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 {

        }

    }

Four. Change the timing of submission to control the number of submissions to improve efficiency

 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);
        }
    }


Guess you like

Origin blog.csdn.net/qq_52655865/article/details/123975787