批处理JDBC
package com.to.go.jdbc.pcl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Statement; import org.omg.Messaging.SyncScopeHelper; import com.to.go.jdbc.domain.Stu; import com.to.go.jdbc.util.JDBCUtil; //批处理 public class Pcl { public static void main(String[] args) throws Exception { // Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/j" + "dbc_db?rewriteBatchedStatements=true", "root", "root"); String sql = "INSERT INTO stu(id,name,age,class_id) VALUES (DEFAULT,?,?,?)"; //获取预编译对象 PreparedStatement ps = conn.prepareStatement(sql); //预编译 1 ,2 ,3 代表坑的位置 long begin = System.currentTimeMillis(); Stu stu = new Stu(); for (int i = 0; i < 100000; i++) { ps.setString(1,"疯狂_"+i+"_号"); ps.setInt(2,i++); ps.setInt(3, i); //添加到批处理 ps.addBatch(); } //执行批处理 ps.executeBatch(); System.out.println("花费时间:" +( System.currentTimeMillis() - begin ) ); //关闭资源 JDBCUtil.close(conn, ps, null); } }
花费时间:369
传播知识,分享快乐!
作者:IT_BULL
出处:http://www.cnblogs.com/itBulls/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
博客园-博客园。