这两个对象的区别:
1.Statement它更适合执行不同sql的批处理,它没有提供预处理功能,性能比较低。
2.PreparedStatement它适合执行相同的批处理,它提供了预处理功能,属性比较高。 
          /**
      * @param args
      * @throws SQLException
      * @throws ClassNotFoundException
      */
     public static void main(String[] args) throws ClassNotFoundException,
                SQLException {
            // 定义sql 语句
           String sql1 = "create table person(id int,name varchar(20))";
           String sql2 = "insert into person values(1,'tom')";
           String sql3 = "insert into person values(2,'fox')";
           String sql4 = "insert into person values(3,'tony')";
           String sql5 = "update person set name='张三' where id=1";
           String sql6 = "delete from person where id=3";
 
           Connection conn = jdbcUtils.getConnection();
           Statement st = conn.createStatement();
 
            // 添加批处理sql
           st.addBatch(sql1);
           st.addBatch(sql2);
           st.addBatch(sql3);
           st.addBatch(sql4);
           st.addBatch(sql5);
           st.addBatch(sql6);
 
            // 执行批处理sql
           st.executeBatch();
           st.clearBatch();
           st.close();
            conn.close();
 
     }
 
 
使用版本高一点的 jdbc的jar包时加入参数可开启缓存在url中加参数:
?useServerPrepStmts=true&cachePrepStmts=true&rewriteBatchedStatements=true
     /**
      * @param args
      * @throws SQLException
      * @throws ClassNotFoundException
      */
     public static void main(String[] args) throws ClassNotFoundException,
                SQLException {
           String sqlString = "insert into person values(?,?)";
           Connection conn = jdbcUtils. getConnection();
           PreparedStatement pst = conn.prepareStatement(sqlString);
            long l = System. currentTimeMillis();
            for ( int i = 0; i < 10000; i++) {
                pst.setInt(1, i);
                pst.setString(2, "name" + i);
                pst.addBatch();
                 if (i % 1000 == 0) {
                     pst.executeBatch();
                     pst.clearBatch(); // 清空缓存
                }
           }
           pst.executeBatch();
           pst.close();
           conn.close();
           System. out.println(System. currentTimeMillis() - l);
     }

最新文章

  1. 破解Java to C# Converter
  2. [转]从JVM角度看线程安全与垃圾收集
  3. BroadcastReceiver study
  4. 线性表基本维护[ACM]
  5. for语句的用法
  6. 8VC Venture Cup 2016 - Elimination Round (C. Block Towers)
  7. PHP根据数组的值分组
  8. asp.net 调用天气所遇到的问题
  9. 几种经典的数据排序及其Java实现
  10. 详解ES6中的 let 和const
  11. 深入理解JSP
  12. Compass实战 站内搜索
  13. Gradle 1.12用户指南翻译——第二十六章. War 插件
  14. 安卓开发:UI组件-RadioButton和复选框CheckBox
  15. GiBbook实用配置以及插件
  16. monkey Test 环境配置
  17. 同步I/O、异步I/O与阻塞I/O、非阻塞I/O的区别
  18. spring-boot (四) springboot+mybatis多数据源最简解决方案
  19. Servlet中request对象得到路径问题
  20. php毫秒时间戳

热门文章

  1. UIRefreshControl自动刷新
  2. SVG 2D入门10 - 滤镜
  3. golang flag包简单例子
  4. UIkit框架之UIcollection
  5. HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)
  6. UVa 10129单词(欧拉回路)
  7. UltraEdit 所有快捷键 说明
  8. day17算法
  9. c++代码美化
  10. foreach属性-动态-mybatis中使用map类型参数,其中key为列名,value为列值