当需要成批插入或者更新记录时,可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率
JDBC的批量处理语句包括下面三个方法:
addBatch(String):添加需要批量处理的SQL语句或是参数;executeBatch():执行批量处理语句;clearBatch():清空缓存的数据通常我们会遇到两种批量执行SQL语句的情况:
多条SQL语句的批量处理;一个SQL语句的批量传参;5.2 高效的批量插入举例:向数据表中插入20000条数据
数据库中提供一个goods表。创建如下:CREATE table goods(ID INT PRIMARY KEY auto_INCREMENT,name VARCHAR(20));
5.2.1 实现层次一:使用StatementConnection conn = JDBCUtils.getConnection();Statement st = conn.createStatement();for(int i = 1;i <= 20000;i++){ String sql = "insert into goods(name) values('name_' + "+ i +")"; st.executeUpdate(sql);}
5.2.2 实现层次二:使用PreparedStatementlong start = System.currentTimeMillis(); Connection conn = JDBCUtils.getConnection(); String sql = "insert into goods(name)values(?)";PreparedStatement ps = conn.prepareStatement(sql);for(int i = 1;i <= 20000;i++){ ps.setString(1,"name_" + i); ps.executeUpdate();} long end = System.currentTimeMillis();System.out.println("花费的时间为:" + (end - start));//82340 JDBCUtils.closeResource(conn,ps);
5.2.3 实现层次三/* * 修改1: 使用 addBatch() / executeBatch() / clearBatch() * 修改2:MysqL服务器默认是关闭批处理的,我们需要通过一个参数,让MysqL开启批处理的支持。 * ?rewriteBatchedStatements=true 写在配置文件的url后面 * 修改3:使用更新的MysqL 驱动:mysql-connector-java-5.1.37-bin.jar * */@Testpublic voID testInsert1() throws Exception{ long start = System.currentTimeMillis(); Connection conn = JDBCUtils.getConnection(); String sql = "insert into goods(name)values(?)"; PreparedStatement ps = conn.prepareStatement(sql); for(int i = 1;i <= 1000000;i++){ ps.setString(1,"name_" + i); //1.“攒”sql ps.addBatch(); if(i % 500 == 0){ //2.执行 ps.executeBatch(); //3.清空 ps.clearBatch(); } } long end = System.currentTimeMillis(); System.out.println("花费的时间为:" + (end - start));//20000条:625 //1000000条:14733 JDBCUtils.closeResource(conn,ps);}
5.2.4 实现层次四/** 层次四:在层次三的基础上 *** 作* 使用Connection 的 setautoCommit(false) / commit()*/@Testpublic voID testInsert2() throws Exception{ long start = System.currentTimeMillis(); Connection conn = JDBCUtils.getConnection(); //1.设置为不自动提交数据 conn.setautoCommit(false); String sql = "insert into goods(name)values(?)"; PreparedStatement ps = conn.prepareStatement(sql); for(int i = 1;i <= 1000000;i++){ ps.setString(1,"name_" + i); //1.“攒”sql ps.addBatch(); if(i % 500 == 0){ //2.执行 ps.executeBatch(); //3.清空 ps.clearBatch(); } } //2.提交数据 conn.commit(); long end = System.currentTimeMillis(); System.out.println("花费的时间为:" + (end - start));//1000000条:4978 JDBCUtils.closeResource(conn,ps);}
总结 以上是内存溢出为你收集整理的【JDBC第5章】批量插入全部内容,希望文章能够帮你解决【JDBC第5章】批量插入所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)