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