java批量insert入mysql数据库


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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM