关闭

Java一次性查询几十万 几百万数据解决办法

发表于:2012-2-28 09:28

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:yjflinchong    来源:51Testing软件测试网采编

  Java查询一次性查询几十万,几百万数据解决办法。

  很早的时候写工具用的一个办法,当时是用来把百万数据打包成rar文件。

  所以用了个笨办法。 希望高手指导一下,有什么好方法没有啊。

  1、先批量查出所有数据,例子中是一万条一批。

  2、在查出数据之后把每次的数据按一定规则存入本地文件。

  3、获取数据时,通过批次读取,获得大批量数据。

  以下是查询数据库。按批次查询

  1. public static void  getMonthDataList() {  
  2.         ResultSet rs = null;  
  3.         Statement stat = null;  
  4.         Connection conn = null;  
  5.         List<DataBean> list = new ArrayList<DataBean>();  
  6.         try {  
  7.             conn = createConnection();  
  8.             if(conn!=null){  
  9.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  10.                 SimpleDateFormat timesdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  11.                 String nowDate = sdf.format(new Date());  
  12.                 Config.lasttimetext = timesdf.format(new Date());  
  13.                 String lastDate = sdf.format(CreateData.addDaysForDate(new Date(), 30));  
  14.                 stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
  15.                 int lastrow = 0;  
  16.                 int datanum = 0;  
  17.                 String countsql = "SELECT count(a.id) FROM trip_special_flight a" +  
  18.                 " where a.dpt_date >= to_date('"+nowDate+"','yyyy-mm-dd') " +  
  19.                 "and a.dpt_date <= to_date('"+lastDate+"','yyyy-mm-dd') and rownum>"+lastrow+" order by a.get_time  desc";  
  20.                 rs = stat.executeQuery(countsql);  
  21.                 while (rs.next()) {  
  22.                     datanum = rs.getInt(1);  
  23.                 }  
  24.                 int onerun = 10000;  
  25.                 int runnum = datanum%onerun==0?(datanum/onerun):(datanum/onerun)+1;  
  26.                 for(int r =0;r<runnum;r++){  
  27.                     System.out.println("getMonthDataList--"+datanum+" 开始查询第"+(r+1)+"批数据");  
  28.                     String sql = "SELECT * FROM  (SELECT rownum rn, a.dpt_code, a.arr_code,a.dpt_date,a.airways,a.flight," +  
  29.                     "a.cabin,a.price FROM trip_special_flight a" +  
  30.                     " where a.dpt_date >= to_date('"+nowDate+"','yyyy-mm-dd') " +  
  31.                     "and a.dpt_date <= to_date('"+lastDate+"','yyyy-mm-dd')  order by rownum  asc) WHERE rn > "+lastrow;  
  32.                     stat.setMaxRows(onerun);  
  33.                     stat.setFetchSize(1000);  
  34.                     rs = stat.executeQuery(sql);  
  35.                     String text = "";  
  36.                     int i = 1;  
  37.                     while (rs.next()) {  
  38.                         text += rs.getString(2)+"|"+rs.getString(3)+"|"+rs.getDate(4)+"|"+rs.getString(5)+"|"+rs.getString(6)+"|"+rs.getString(7)+"|"+rs.getString(8)+"||";  
  39.                         if(i%1000==0){  
  40.                             FileUtil.appendToFile(Config.tempdatafile, text);  
  41.                             text = "";  
  42.                         }  
  43.                         i++;  
  44.                     }  
  45.                     if(text.length()>10){  
  46.                         FileUtil.appendToFile(Config.tempdatafile, text);  
  47.                     }  
  48.                     lastrow+=onerun;  
  49.                 }  
  50.             }  
  51.         } catch (Exception e) {  
  52.             e.printStackTrace();  
  53.         } finally {  
  54.             closeAll(rs, stat, conn);  
  55.         }  
  56.     }

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号