@@ -288,10 +288,11 @@ <h2>Header auto detection</h2>
288288 </ pre >
289289 This will use the values from the first record as header names and skip the first record when iterating.
290290 </ section >
291- < section >
292- < h2 > Printing with headers</ h2 >
293- < p > To print a CSV file with headers, you specify the headers in the format:</ p >
294- < pre >
291+ </ section >
292+ < section >
293+ < h1 > Printing with headers</ h1 >
294+ < p > To print a CSV file with headers, you specify the headers in the format:</ p >
295+ < pre >
295296 < code >
296297Appendable out = ...;
297298CSVPrinter printer = CSVFormat.DEFAULT.builder()
@@ -300,8 +301,8 @@ <h2>Printing with headers</h2>
300301 .print(out);
301302 </ code >
302303 </ pre >
303- < p > To print a CSV file with JDBC column labels, you specify the ResultSet in the format:</ p >
304- < pre >
304+ < p > To print a CSV file with JDBC column labels, you specify the ResultSet in the format:</ p >
305+ < pre >
305306 < code >
306307try (ResultSet resultSet = ...) {
307308 CSVPrinter printer = CSVFormat.DEFAULT.builder()
@@ -311,7 +312,31 @@ <h2>Printing with headers</h2>
311312}
312313 </ code >
313314 </ pre >
314- </ section >
315+ </ section >
316+ < section >
317+ < h1 > Exporting JDBC Result Sets</ h1 >
318+ < p >
319+ To export row data from a JDBC
320+ < code > ResultSet</ code >
321+ , use
322+ < code > CSVPrinter.printRecords(ResultSet)</ code >
323+ :
324+ </ p >
325+ < pre >
326+ < code >
327+ final StringWriter sw = new StringWriter();
328+ final CSVFormat csvFormat = CSVFormat.DEFAULT;
329+ try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "")) {
330+ try (Statement stmt = connection.createStatement();
331+ CSVPrinter printer = new CSVPrinter(sw, csvFormat);
332+ ResultSet resultSet = stmt.executeQuery("select ID, NAME, TEXT, BIN_DATA from TEST")) {
333+ printer.printRecords(resultSet);
334+ }
335+ }
336+ final String csv = sw.toString();
337+ System.out.println(csv);
338+ </ code >
339+ </ pre >
315340 </ section >
316341</ body >
317342</ html >
0 commit comments