0

I am trying to loop across a result set and create csv chunks by row numbers. WriteAll causing loop stopped, so only first file is filled with data.

Any ideas? Thanks.

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;
    }
}
DJK27
  • 1
  • 1
    Your question isn't exactly clear, but it appears you are ONLY writing data if counter == 10. So, this will only print out every 10th row from your record set. How big is your record set? Seeing as you immediately set counter=0 in that inner while loop, it might as well just be an _if_ block instead. – Jamie Sep 20 '18 at 14:49
  • Also `writeAll` probably reads and exhausts the whole resultset. – Arnaud Sep 20 '18 at 14:56
  • @Arnaud you are right, any ideas how to split resultset in multiple csv files? Thanks. – DJK27 Sep 20 '18 at 15:20
  • @Jamie thank you for answer! Yes, you are right, it should be "if" block! And, it is not printing 10th row, but first 10 rows. That is exactly what should do, but it should continue populating next 10 rows in another file, but that is not happening. As Arnaud say, writeAll reads whole resultset, but im wondering how to fix it. Thanks. – DJK27 Sep 20 '18 at 15:32

0 Answers0