2525
2626import java .io .Flushable ;
2727import java .io .IOException ;
28+ import java .sql .ResultSet ;
29+ import java .sql .SQLException ;
2830
2931/**
3032 * Prints values in a CSV format.
3133 */
3234public class CSVPrinter {
33-
35+
3436 /** The place that the values get written. */
3537 private final Appendable out ;
3638 private final CSVFormat format ;
@@ -43,7 +45,7 @@ public class CSVPrinter {
4345 * <p/>
4446 * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats
4547 * (encapsulation and escaping with a different character) are not supported.
46- *
48+ *
4749 * @param out
4850 * stream to which to print.
4951 * @param format
@@ -62,7 +64,7 @@ public CSVPrinter(final Appendable out, final CSVFormat format) {
6264 // ======================================================
6365
6466 /**
65- * Outputs a blank line
67+ * Outputs a the line separator.
6668 */
6769 public void println () throws IOException {
6870 out .append (format .getLineSeparator ());
@@ -71,7 +73,7 @@ public void println() throws IOException {
7173
7274 /**
7375 * Flushes the underlying stream.
74- *
76+ *
7577 * @throws IOException
7678 */
7779 public void flush () throws IOException {
@@ -83,7 +85,7 @@ public void flush() throws IOException {
8385 /**
8486 * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
8587 * characters will be escaped.
86- *
88+ *
8789 * @param values
8890 * values to output.
8991 */
@@ -97,7 +99,7 @@ public void printRecord(final Object... values) throws IOException {
9799 /**
98100 * Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
99101 * characters will be escaped.
100- *
102+ *
101103 * @param values
102104 * values to output.
103105 */
@@ -109,12 +111,12 @@ public void printRecord(final Iterable<?> values) throws IOException {
109111 }
110112
111113 /**
112- * Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line and
113- * occupy a least one full line. The character specified to start comments and a space will be inserted at the
114+ * Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line
115+ * and occupy a least one full line. The character specified to start comments and a space will be inserted at the
114116 * beginning of each new line in the comment.
115117 * <p/>
116118 * If comments are disabled in the current CSV format this method does nothing.
117- *
119+ *
118120 * @param comment
119121 * the comment to output
120122 */
@@ -293,11 +295,11 @@ void printAndQuote(final CharSequence value, final int offset, final int len) th
293295 /**
294296 * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed if
295297 * checkForEscape==true
296- *
298+ *
297299 * @param object
298300 * value to output.
299- * @throws IOException
300- * If an I/O error occurs
301+ * @throws IOException
302+ * If an I/O error occurs
301303 */
302304 public void print (Object object , final boolean checkForEscape ) throws IOException {
303305 // null values are considered empty
@@ -313,11 +315,11 @@ public void print(Object object, final boolean checkForEscape) throws IOExceptio
313315
314316 /**
315317 * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
316- *
318+ *
317319 * @param value
318320 * value to be output.
319- * @throws IOException
320- * If an I/O error occurs
321+ * @throws IOException
322+ * If an I/O error occurs
321323 */
322324 public void print (final Object value ) throws IOException {
323325 print (value , true );
@@ -328,8 +330,8 @@ public void print(final Object value) throws IOException {
328330 *
329331 * @param values
330332 * the values to print.
331- * @throws IOException
332- * If an I/O error occurs
333+ * @throws IOException
334+ * If an I/O error occurs
333335 */
334336 public void printRecords (Object [] values ) throws IOException {
335337 for (Object value : values ) {
@@ -348,8 +350,8 @@ public void printRecords(Object[] values) throws IOException {
348350 *
349351 * @param values
350352 * the values to print.
351- * @throws IOException
352- * If an I/O error occurs
353+ * @throws IOException
354+ * If an I/O error occurs
353355 */
354356 public void printRecords (Iterable <?> values ) throws IOException {
355357 for (Object value : values ) {
@@ -362,4 +364,22 @@ public void printRecords(Iterable<?> values) throws IOException {
362364 }
363365 }
364366 }
367+
368+ /**
369+ * Prints all the objects in the given JDBC result set.
370+ *
371+ * @param resultSet result set
372+ * the values to print.
373+ * @throws IOException
374+ * If an I/O error occurs
375+ */
376+ public void printRecords (ResultSet resultSet ) throws SQLException , IOException {
377+ int columnCount = resultSet .getMetaData ().getColumnCount ();
378+ while (resultSet .next ()) {
379+ for (int i = 1 ; i <= columnCount ; i ++) {
380+ print (resultSet .getString (i ));
381+ }
382+ println ();
383+ }
384+ }
365385}
0 commit comments