这里的表是没加索引的,加了索引,效率会变慢,但是还是很高效。
 long startTime=System.currentTimeMillis();
        
        Connection conn=null;
        try{
            conn=getConnection();
            conn.setAutoCommit(false);
            
            PreparedStatement stmt=conn.prepareStatement("INSERT INTO product_tmp VALUES (?,?,?,?)");
            System.out.println("数据大小:"+datas.size());        //1000000
            int num=0;
            for(
Values v:datas){
                num++;
                stmt.setInt(1, v.getId());
                stmt.setString(2, v.getStr1());
                stmt.setString(3, v.getStr2());
                stmt.setString(4, v.getStr3());
                stmt.addBatch();
                //注意: 每5万,提交一次;这里不能一次提交过多的数据,我测试了一下,6万5000是极限,6万6000就会出问题,插入的数据量不对。
                if(num>50000){
                    stmt.executeBatch();
                    conn.commit();
                    num=0;
                }
            }
            stmt.executeBatch();
            conn.commit();
        }catch(Exception e){
            conn.rollback();
            e.printStackTrace();
        }finally{
            closeConnection(conn);
            long endTime=System.currentTimeMillis();
            System.out.println("方法执行时间:"+(endTime-startTime)+"ms");
        }
转:http://www.ablanxue.com/prone_13223_1.html