Skip to content

Commit 4df7326

Browse files
committed
Add documentation to limit rows from JDBC ResultSets
1 parent 44ec701 commit 4df7326

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

src/main/javadoc/overview.html

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,15 @@ <h1>Printing with headers</h1>
314314
</pre>
315315
</section>
316316
<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>
317+
<h1>Working with JDBC</h1>
318+
<section>
319+
<h2>Exporting JDBC Result Sets</h2>
320+
<p>
321+
To export row data from a JDBC
322+
<code>ResultSet</code>
323+
, use <a href="org/apache/commons/csv/CSVPrinter.html#printRecords(java.sql.ResultSet)">CSVPrinter.printRecords(ResultSet)</a> :
324+
</p>
325+
<pre>
326326
<code>
327327
final StringWriter sw = new StringWriter();
328328
final CSVFormat csvFormat = CSVFormat.DEFAULT;
@@ -337,6 +337,39 @@ <h1>Exporting JDBC Result Sets</h1>
337337
System.out.println(csv);
338338
</code>
339339
</pre>
340+
</section>
341+
<section>
342+
<h2>Limiting rows from JDBC Result Sets</h2>
343+
<p>SQL lets you limit how many rows a SELECT statement returns with the LIMIT clause.</p>
344+
<p>
345+
When you can't or don't want to change the SQL used to generate rows, JDBC lets you limit how many rows a JDBC Statement returns with the <a
346+
href="https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/Statement.html#setMaxRows(int)">Statement.setMaxRows(int)</a> method.
347+
</p>
348+
<p>
349+
When you get a JDBC ResultSet from an API like <a
350+
href="https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/DatabaseMetaData.html#getProcedures(java.lang.String,java.lang.String,java.lang.String)">
351+
DatabaseMetaData.getProcedures(...)</a>, there is no SQL or JDBC Statement to use to set a limit, the ResultSet class does not have an API to limit rows.
352+
</p>
353+
<p>
354+
To simplify limiting ResultSet rows, Commons CVS offers the <a href="org/apache/commons/csv/CSVFormat.Builder.html#setMaxRows(long)">CSVFormat.Builder.setMaxRows(long)</a>
355+
method. For example:
356+
</p>
357+
<pre>
358+
<code>
359+
CSVFormat csvFormat = CSVFormat.DEFAULT
360+
.setMaxRows(5_000)
361+
.get();
362+
try (ResultSet resultSet = ...) {
363+
csvFormat.printer().printRecords(resultSet);
364+
}
365+
</code>
366+
</pre>
367+
<p>
368+
Using the above, calling <a href="org/apache/commons/csv/CSVPrinter.html#printRecords(java.sql.ResultSet)">CSVPrinter.printRecords(ResultSet)</a> will
369+
limit the row count to the maximum number of rows specified in setMaxRows().
370+
</p>
371+
<p>Note that setMaxRows() works with the other methods that print a sequence of records.</p>
372+
</section>
340373
</section>
341374
</body>
342375
</html>

0 commit comments

Comments
 (0)