0


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

一.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插入

  1. //批量插入方式二:使用PreparedStatement插入
  2. public void testInsertInfo() throws Exception {
  3. Connection conn = null;
  4. PreparedStatement pst = null;
  5. String sql = "insert into user_tab(id,name) values(?,?)";
  6. try {
  7. long start = System.currentTimeMillis();
  8. conn = JDBCUtils.getConnection();
  9. pst = conn.prepareStatement(sql);
  10. for (int i = 1; i <20000 ; i++) {
  11. pst.setObject(1,i);
  12. pst.setObject(2,"name");
  13. pst.execute();
  14. }
  15. long end = System.currentTimeMillis();
  16. System.out.println("采用preparedStatement方式插入20000条数据花费的时间为:"+(end - start));
  17. }catch (Exception e){
  18. e.printStackTrace();
  19. }finally {
  20. JDBCUtils.closeResource(conn,pst);
  21. }
  22. }

三.使用batch缓存插入

  1. //批量插入方式三:使用批处理插入
  2. /*
  3. 1.使用 添加batch :addBatch() 执行batch:executeBatch() 清空Batch(): clearBatch();
  4. 2.在连接数据的url后面配置这句话:
  5. ?rewriteBatchedStatements=true
  6. 3.批量插入数据使用的jdbc.jar必须实在5.7版本以上才支持
  7. */
  8. @Test
  9. public void testInsertInfo2() {
  10. Connection conn = null;
  11. PreparedStatement pst = null;
  12. String sql = "insert into user_tab(id,name) values(?,?)";
  13. try{
  14. long start = System.currentTimeMillis();
  15. conn = JDBCUtils.getConnection();
  16. pst = conn.prepareStatement(sql);
  17. for (int i = 1; i <=20000 ; i++) {
  18. pst.setObject(1,i);
  19. pst.setObject(2,"name"+i);
  20. //1,添加batch
  21. pst.addBatch();
  22. //2.执行batch()
  23. if (i % 500 == 0){
  24. pst.executeBatch();
  25. //清空batch
  26. pst.clearBatch();
  27. }
  28. }
  29. long end = System.currentTimeMillis();
  30. System.out.println("采用batch插入20000条数据花费时间为:"+ (end - start));
  31. }catch (Exception e){
  32. e.printStackTrace();
  33. }finally {
  34. }
  35. }

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

  1. public void testInsertInfo3(){
  2. Connection conn = null;
  3. PreparedStatement pst = null;
  4. String sql = "insert into user_tab(id ,name) values(?,?)";
  5. try {
  6. long start = System.currentTimeMillis();
  7. conn = JDBCUtils.getConnection();
  8. conn.setAutoCommit(false);
  9. pst = conn.prepareStatement(sql);
  10. for (int i = 1; i <= 200000; i++) {
  11. pst.setObject(1,i);
  12. pst.setObject(2,"name"+i);
  13. //添加缓存
  14. pst.addBatch();
  15. if(i % 500 == 0){
  16. //执行缓存
  17. pst.executeBatch();
  18. //清空缓存
  19. pst.clearBatch();
  20. }
  21. }
  22. conn.commit();
  23. long end = System.currentTimeMillis();
  24. System.out.println("采用设置不允许自动提交的方式的花费的时间:"+(end - start));
  25. }catch (Exception e){
  26. e.printStackTrace();
  27. }finally {
  28. JDBCUtils.closeResource(conn,pst);
  29. }
  30. }


本文转载自: https://blog.csdn.net/qq_52655865/article/details/123975787
版权归原作者 余扯 所有, 如有侵权,请联系我们删除。

“【笔试面试考点】PreparedStatement和Statement的区别与联系&amp;&amp;批量插入数据的优化”的评论:

还没有评论