How to set the maximum rows to read in a query?
If you want to limit the result of your query you can use the
Statement.setMaxRows(int max) method call. This call will allow the
ResultSet object to contains a maximum number of records specified in the parameter of the
Another way to limit the number of data returned in a query is to use the database specific command such as the MySQL
When running the code we'll see that only 5 records was read from the
Products table instead of 10 records. This is the result of setting the maximum rows in the
Below is the output of our code.
Total number of Products: 10
Data read after the MaxRows is set.
ID: 1, CODE: P0001, NAME: Product 01, PRICE: 19.99, QTY: 10
ID: 2, CODE: P0002, NAME: Product 02, PRICE: 29.99, QTY: 20
ID: 3, CODE: P0003, NAME: Product 03, PRICE: 39.99, QTY: 30
ID: 4, CODE: P0004, NAME: Product 04, PRICE: 49.99, QTY: 40
ID: 5, CODE: P0005, NAME: Product 05, PRICE: 59.99, QTY: 50