批处理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

posted @ 2018-05-27 23:14  马鞍山  阅读(126)  评论(0编辑  收藏  举报