从结果集中创建CSV块

时间:2018-09-20 14:37:14

标签: java csv resultset opencsv

我试图遍历结果集并按行号创建csv块。 WriteAll导致循环停止,因此只有第一个文件填充了数据。

有什么想法吗?谢谢。

PreparedStatement stmt = con.prepareStatement(query);
ResultSet rs = stmt.executeQuery();

int counter = 0;
int i = 0;
Path myPath = Paths.get(filname + "_" + i++ + ".csv");
CSVWriter writer = new CSVWriter(Files.newBufferedWriter(myPath, StandardCharsets.UTF_8),
        SEPARATOR_CHAR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER,
        CSVWriter.DEFAULT_LINE_END);

while (rs.next()) {
    counter++;

    while (counter == 10) {
        System.out.println(counter);
        writer.writeAll(rs, true);
        writer.flush();
        writer.close();

        myPath = Paths.get(filname + "_" + i++ + ".csv");
        writer = new CSVWriter(Files.newBufferedWriter(myPath, StandardCharsets.UTF_8),
            SEPARATOR_CHAR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER,
            CSVWriter.DEFAULT_LINE_END);
        counter = 0;
    }
}

0 个答案:

没有答案