2017-01-03 66 views
-2

我想打印從JDBC連接而得到的結果集的結果爲CSV並希望它打印每個結果aloop.So設置我的代碼如下:得到一個循環

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.io.PrintWriter; 

import java.sql.*; 
import java.util.*; 

public class zone { 

    List<model> listOfBlogs; 

    public zone() { 
     Connection conn = null; 
     listOfBlogs = new ArrayList<model>(); 

     // connect to the database 

     conn = connectToDatabaseOrDie(); 

     // get the data 
     populateListOfTopics(conn, listOfBlogs); 

     //print the data 
     printTopics(listOfBlogs); 
    } 

    public List<model> printTopics(List<model> listOfBlogs) { 
     Iterator<model> it = listOfBlogs.iterator(); 
     while (it.hasNext()) { 
      model blog = (model) it.next(); 
      System.out.println(blog.day + " " + blog.hour + " " + blog.zone_id); 
     } 
     return listOfBlogs; 
    } 

    private void populateListOfTopics(Connection conn, List<model> listOfBlogs) { 
     try { 
      for (int j = 9; j < 10; j++) { 
       for (int i = 10; i < 12; i++) { 

        String sql = "SELECT day,hour,zone_id FROM public.zone_block_table where day=? and hour=?"; 
        PreparedStatement pstmt = conn.prepareStatement(sql); 
        pstmt.setInt(1, j); 
        pstmt.setInt(2, i); 

        ResultSet rs = pstmt.executeQuery(); 

        while (rs.next()) { 
         model blog = new model(); 

         blog.day = rs.getInt("day"); 
         blog.hour = rs.getInt("hour"); 
         blog.zone_id = rs.getInt("zone_id"); 

         listOfBlogs.add(blog); 
        } 
        convertToCsv(rs, i, j); 

        rs.close(); 
        pstmt.close(); 
       } 
      } 
     } catch (SQLException se) { 
      System.err.println("Threw a SQLException creating the list of state."); 
      System.err.println(se.getMessage()); 
     } catch (Exception e) { 
      System.out.println("Err"); 
      e.printStackTrace(); 
     } 
    } 

    private Connection connectToDatabaseOrDie() { 
     Connection conn = null; 
     try { 
      Class.forName("org.postgresql.Driver"); 
      String url = "jdbc:postgresql://localhost:5432/mvc_data_base"; 
      conn = DriverManager.getConnection(url, "postgres", "122333"); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     return conn; 
    } 

    public void convertToCsv(ResultSet rs, int k, int l) throws SQLException, FileNotFoundException { 
     PrintWriter csvWriter = new PrintWriter(new File("state_" + k + "_" + l + ".csv")); 
     ResultSetMetaData meta = rs.getMetaData(); 
     int numberOfColumns = meta.getColumnCount(); 
     String dataHeaders = "\"" + meta.getColumnName(1) + "\""; 
     for (int i = 2; i < numberOfColumns + 1; i++) { 
      dataHeaders += ",\"" + meta.getColumnName(i) + "\""; 
     } 
     csvWriter.println(dataHeaders); 
     while (rs.next()) { 
      String row = "\"" + rs.getString(1) + "\""; 
      for (int i = 2; i < numberOfColumns + 1; i++) { 
       row += ",\"" + rs.getString(i) + "\""; 
      } 
      csvWriter.println(row); 
     } 
     csvWriter.close(); 
    } 
} 

但結果集打印完美。但我找不到任何目的地生成的任何csv文件。任何幫助表示讚賞。

+0

也許不是你所期望的路徑創建的文件,試圖打印'新的文件( 「狀態_」 + K + 「_」 + 1 + 「CSV」)。getAbsolutePath ()'看看它將在哪裏。 – Berger

+0

在調用'convertToCsv'方法之前,您已經使用了整個結果集:沒有什麼可寫的了。 –

+0

是的,我知道了。因爲我認爲我們可以把結果集作爲一個數據結構,我完全錯了。謝謝很多 – RKR

回答

0

有一種使用opencsv從resultset創建CSV文件的Direct方法。嘗試

CSVWriter writer = new CSVWriter(new FileWriter(csvtemp), ',' ,CSVWriter.NO_QUOTE_CHARACTER , CSVWriter.NO_ESCAPE_CHARACTER , "\r\n");

writer.writeAll(myResultSet, true); 
writer.close(); 
+0

是的,這將工作,但我不想使用任何第三方庫。所以,無論如何,謝謝。 – RKR