最近使用jdbc方式查詢數據,保存爲csv文件中。固然你能夠在pl/sql中直接查出來,copy to excel就行了。但我想經過程序實現sql
1 @Test 2 public void test() throws IOException { 3 BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\yhzh\\Desktop\\zh_20160913"))); 4 String tmp=null; 5 List<String> nos=new ArrayList<String>(); 6 while((tmp=reader.readLine()) !=null) 7 nos.add(tmp); 8 9 Connection con = null;// 建立一個數據庫鏈接 10 PreparedStatement pre = null;// 建立預編譯語句對象,通常都是用這個而不用Statement 11 PreparedStatement pre2 = null; 12 ResultSet result = null;// 建立一個結果集對象 13 BufferedWriter csvWriter=null; 14 try 15 { 16 String tag=(new SimpleDateFormat("hhmmss")).format(new Date()); 17 csvWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("C:\\Users\\yhzh\\Desktop\\貸後還款計劃_"+tag+".csv")), "utf-8")); 18 19 Class.forName("oracle.jdbc.driver.OracleDriver"); 20 //:6006/hotfix 21 String url = "jdbc:oracle:thin:@//*.*.*.*:16030/zcgl",user = "*",password = "*"; 22 con = DriverManager.getConnection(url, user, password);// 獲取鏈接 23 String sql="select max(lr_id) from t_loan_request where LR_REQUESTSTATUS ='2' and lr_applyid=?"; 24 /*String sql = 27 "select req.LR_APPLYID,CURR_PERIODS,REPAY_DAY,\n" + 28 "(NEEDREPAY_PRINCIPAL+NEEDREPAY_INTEREST+NEEDREPAY_ADMIFEE+nvl(RISK_AMOUNT,0)+nvl(SERVICEFEE,0)+nvl(PARKINGFEE,0))NEEDREPAY_TOTAL,\n" + 29 "PERIOD_REPAY_AMOUNT \n" + 30 "from(\n" + 31 " select lr_id,LR_APPLYID from t_loan_request t \n" + 32 " where t.LR_REQUESTSTATUS ='2' and t.creater='PostLoanOuterAction'\n" + 33 " order by lr_id desc)req\n" + 34 "left join t_repay_plan rp\n" + 35 "on req.lr_id=rp.lr_id\n" + 36 "order by req.lr_id,CURR_PERIODS ";// 預編譯語句,「?」表明參數*/ 37 pre = con.prepareStatement(sql); 38 pre2=con.prepareStatement("select CURR_PERIODS,REPAY_DAY,\n" + 39 "(NEEDREPAY_PRINCIPAL+NEEDREPAY_INTEREST+NEEDREPAY_ADMIFEE+nvl(RISK_AMOUNT,0)+nvl(SERVICEFEE,0)+nvl(PARKINGFEE,0))NEEDREPAY_TOTAL,\n" + 40 "PERIOD_REPAY_AMOUNT \n" + 41 "from t_repay_plan\n" + 42 "where lr_id=? " + 43 "order by CURR_PERIODS"); 44 for(String no:nos){ 45 pre.setString(1,no); 46 result = pre.executeQuery(); 47 if(result.next()) { 48 long lrId=result.getLong(1); 49 pre2.setLong(1,lrId); 50 result = pre2.executeQuery(); 51 while (result.next()) { 52 csvWriter.write(no); 53 csvWriter.write(","); 54 csvWriter.write(result.getString(1)); 55 csvWriter.write(","); 56 csvWriter.write(result.getString(2)); 57 csvWriter.write(","); 58 csvWriter.write(result.getString(3)); 59 csvWriter.write(","); 60 csvWriter.write(result.getString(4)); 61 csvWriter.newLine(); 62 } 63 } 64 } 65 66 csvWriter.flush(); 67 } 68 catch (Exception e) 69 { 70 e.printStackTrace(); 71 } 72 finally 73 { 74 try 75 { 76 if(csvWriter !=null) 77 csvWriter.close(); 78 if (result != null) 79 result.close(); 80 if (pre != null) 81 pre.close(); 82 if (con != null) 83 con.close(); 84 System.out.println("數據庫鏈接已關閉!"); 85 } 86 catch (Exception e) 87 { 88 e.printStackTrace(); 89 } 90 } 91 }
先讀取全部編號造成List,後遍歷這個List,先查出id再查詳細數據。這樣的數據csv文件中大約8萬多條數據庫
一條條的來確定慢,若是不按照編號,直接一次查出,數據是9萬多條。速度都很慢!!!oracle
後來想提升下,至少要有個明顯的提高呀。寫文件這塊基本排除了,剩下的疑問就是ResultSet是否拿到了全部結果呢?app
根據網上查到的資料和實際調試,得出答案:ResultSet默認一次取10條數據,怪不得要慢,若是一次所有讀入內存再寫入文件就必定很快了。fetch
怎樣一次讀取全部數據呢?url
sql改成讀取所有spa
1 String sql="select req.LR_APPLYID,CURR_PERIODS,REPAY_DAY,\n" + 2 "(NEEDREPAY_PRINCIPAL+NEEDREPAY_INTEREST+NEEDREPAY_ADMIFEE+nvl(RISK_AMOUNT,0)+nvl(SERVICEFEE,0)+nvl(PARKINGFEE,0))NEEDREPAY_TOTAL,\n" + 3 "PERIOD_REPAY_AMOUNT \n" + 4 "from(\n" + 5 " select lr_id,LR_APPLYID from t_loan_request t \n" + 6 " where t.LR_REQUESTSTATUS ='2' and t.creater='PostLoanOuterAction'\n" + 7 " order by lr_id desc)req\n" + 8 "left join t_repay_plan rp\n" + 9 "on req.lr_id=rp.lr_id\n" + 10 "order by req.lr_id,CURR_PERIODS";
設置PreparedStatement:調試
1 pre = con.prepareStatement(sql); 2 pre.setFetchSize(100000); 3 result = pre.executeQuery(); 4 //result.setFetchSize(100000);
主要是PreparedStatement的 setFetchSize 方法,excel
後來發現ResultSet也有個setFetchSize 方法,也是可行的,只是這個時候resultset中已經有了10條記錄直到循環10次後,再次使用result.next()纔去取100000,fetchSize才起做用code
這樣設置後速度飛快!!!