This week's book giveaways are in the Jython/Python and Object-Oriented programming forums. We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line! See this thread and this one for details.
How do I limit the number of rows displayed in the results page?
Often a database query will yield hundreds or thousands of rows of data.
Trying to display all of them in one webpage could be very slow and resource intensive. Worse, it results in a poor user experience for the person who actually has to scroll through all of this data.
Well-designed websites will return a small subset of the data and provide links to move to the next or last set.
The easiest way to accomplish this is by constraining the number of rows returned from the database query in the first place.
MySQL uses the 'LIMIT' keyword for this.
PostgreSQL uses the 'LIMIT' and 'OFFSET' keywords for this.
Oracle uses the 'ROWNUM' keyword. It doesn't work like LIMIT and OFFSET. You will probably find it strange (this page might help).
DB2 uses the 'ROWNUMBER' 'OVER' combination.
SQL Server uses the 'TOP' keyword, followed by the number of rows from the top of the result.
A typical query in MySQL would look something like:
Because this is part of the JDBC API, it works for all servers.
It should be noted that with both approaches it is undefined which rows are returned. If you want particular rows returned, you should add an "order ..." clause so that the order of rows is defined.
Javaranch has a forum dedicated to JDBC/SQL issues where you can go to read more about formulating SQL statements: JDBC forum