首页 > 代码库 > 使用Statement 的addBatch方法批量导入数据库

使用Statement 的addBatch方法批量导入数据库

 1     try { 2                 boolean auto = conn.getAutoCommit(); 3                 conn.setAutoCommit(false); 4  5                 Statement stat = conn.createStatement(); 6                 Iterator<String> it = sourcetime.iterator(); 7               //  System.out.println("get sourcetime iterator successfully"); 8                 while (it.hasNext()) { 9                     String st = it.next();10                     //System.out.println("this st*_*"+st+"*_*");11                     Pattern subpat = Pattern.compile("[|]"); // 创建Pattern实例12                     String mac_time[] = subpat.split(st);13                     int time = Integer.parseInt(mac_time[1]); // time14                     String mac = mac_time[0];15                    // System.out.println("this::::"+mac+","+gname +","+time);16                     String sql = "insert into edges (source,target,time) values(‘" + mac + "‘,‘" + gname17                             + "‘,‘" + time + "‘)";18                     stat.addBatch(sql);19                 }20                 stat.executeBatch();21                 conn.commit();22                 logger.info("insert "+gname+" data into DB edges finished");23                 conn.setAutoCommit(auto);24             } catch (SQLException e2) {25                 // TODO Auto-generated catch block26                 e2.printStackTrace();27             }

 

使用Statement 的addBatch方法批量导入数据库