How do I get the size of a java.sql.ResultSet?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In Java, the java.sql.ResultSet interface represents a database result set obtained from executing SQL queries via Statement, PreparedStatement, or CallableStatement. Determining the size (or number of rows) directly from a ResultSet can be essential for various reasons such as pagination, data validation, or even just logging the number of records fetched. However, unlike some collections in Java, ResultSet does not provide a direct method to retrieve its size.
Understanding the ResultSet Type
Before diving into the mechanisms to determine the size, understanding the type of ResultSet is crucial. The ResultSet can be of type TYPE_FORWARD_ONLY (which only allows moving forward), TYPE_SCROLL_INSENSITIVE (which allows moving forward and backward but does not reflect changes made by others), or TYPE_SCROLL_SENSITIVE (which allows moving in any direction and reflects changes made by others). The type influences how, or even if, you can count the rows.
Techniques to Determine the Size of a ResultSet
1. Scrolling the ResultSet
If the ResultSet type allows scrolling (i.e., it is either TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE), you can navigate to the last row and use the getRow() method to determine the size:
2. Querying with SQL Count
For a forward-only ResultSet or when you want to avoid the potential overhead of scrolling a large ResultSet, executing a separate SQL COUNT query can be more efficient:
Note: This method requires knowledge of the database schema.
3. Looping Through ResultSet
For TYPE_FORWARD_ONLY result sets, if it is undesirable or impractical to rerun a count query, you might have no choice but to loop through the ResultSet:
Performance and Consequences
These techniques have different performance implications:
- Scrolling: Efficient for smaller result sets but performance degrades as the size increases.
- Count Query: Generally very fast but requires a separate query and correct SQL.
- Looping: Inefficient for large datasets and consumes more time and resources.
Each method's performance and feasibility also depend on the database driver capabilities and DBMS behaviors.
Summary Table
| Method | Compatibility | Performance | Use Case |
Scrolling and getRow() | Scrollable ResultSet | Fast for small data | General purpose, when scrolling is achievable |
SQL COUNT query | All ResultSet types | Varies by DB setup | When table name/schema is known |
Loop through ResultSet | Forward-only ResultSet | Slow for large data | When other methods are not applicable |
Conclusion
In conclusion, determining the size of a java.sql.ResultSet requires understanding both the type of ResultSet and the context in which it is used. Each method has its appropriate use case depending on the requirements and constraints of your database environment and the Java application design. While some methods may be optimal in terms of performance, they might require additional privileges or knowledge about the database schema, making them less versatile or practical in certain scenarios. In designing your approach, consider the balance between implementation complexity, performance needs, and application architecture.
Related reading
- How do I handle Database Connections with Dapper in .NET?
- How do I import CSV file into a MySQL table?
- How do I insert a map into DynamoDB table?
- How do I install command line MySQL client on mac?
- How do I handle RabbitMQ Consumer Cancellation Notification when using Spring ChannelAwareMessageListener
- How do I hide .class files from the Open Resource dialog in Eclipse?
- How do I kill a process in MySQL running within Amazon RDS?
- How do I kill all the processes in Mysql show processlist?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.