首页 > 数据库技术 > 详细

java批量insert入mysql数据库

时间:2016-12-28 23:31:19      阅读:621      评论:0      收藏:0      [点我收藏+]

mysql 批量insert语句为

insert into Table_(col1,col2...) values(val11,val12...),(val11,val12...),...;

java代码示例

jdbc连接串中设置rewriteBatchedStatements=true,

        int step = 50 * 10000;
        Connection conn = ConnectionPool.bds.getConnection();
        conn.setAutoCommit(false);
        String sql = "SET UNIQUE_CHECKS=0";
        Statement st = conn.createStatement();
        st.execute(sql);

        PreparedStatement ps = conn.prepareStatement("insert into henan_enterprise(name) values(?)");
        int count = 0;
        long time0=System.currentTimeMillis();
        for (String s : ne) {
            ps.setString(1, s);
            ps.addBatch();
            if (++count % step == 0) {
                ps.executeBatch();
                long t=System.currentTimeMillis();
                logger.debug("execute "+count+" times!用时:"+LocalUtil.formatTime(t-time0));
            }
        }
        if (ne.size() % step != 0) {
            ps.executeBatch();
            long t=System.currentTimeMillis();
            logger.debug("execute "+count+" times!用时:"+LocalUtil.formatTime(t-time0));
        }
        conn.commit();
        sql = "SET UNIQUE_CHECKS=1";
        st.execute(sql);
        st.close();
        ps.close();
        conn.close();

参考:

http://www.111cn.net/database/mysql/53274.htm

http://elf8848.iteye.com/blog/770032

java批量insert入mysql数据库

原文:http://www.cnblogs.com/windyWu/p/6230715.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!