How do I get the size of a java.sql.ResultSet?
Do a SELECT COUNT(*) FROM ...
query instead.
OR
int size =0;
if (rs != null)
{
rs.last(); // moves cursor to the last row
size = rs.getRow(); // get row id
}
In either of the case, you won't have to loop over the entire data.
How to get the size of a ResultSet
ResultSet.getFetchSize()
doesn't return the number of results. JDBC 2.0 also allows you to specify the number of rows fetched with each database round trip for a query, and this number is referred to as the fetch size
. You can read about it here
And about getting number of rows in result set you can try
int size= 0;
if (resultSet!= null)
{
resultSet.beforeFirst();
resultSet.last();
size = resultSet.getRow();
}
How to get the size of a java.sql.ResultSet?
What's the proper way of doing this?
Map it to a List<Entity>
. Since your code is far from self-documenting (you're using indexes instead of column names), I can't give a well suited example. So I'll take a Person
as example.
First create a javabean class representing whatever a single row contains.
public class Person {
private Long id;
private String firstName;
private String lastName;
private Date dateOfBirth;
// Add/generate c'tors/getters/setters/equals/hashcode and other boilerplate.
}
(a bit decent IDE like Eclipse can autogenerate them)
Then let JDBC do the following job.
List<Person> persons = new ArrayList<Person>();
while (resultSet.next()) {
Person person = new Person();
person.setId(resultSet.getLong("id"));
person.setFirstName(resultSet.getString("fistName"));
person.setLastName(resultSet.getString("lastName"));
person.setDataOfBirth(resultSet.getDate("dateOfBirth"));
persons.add(person);
}
// Close resultSet/statement/connection in finally block.
return persons;
Then you can just do
int size = persons.size();
And then to substitute your code example
for (int i = 0; i < persons.size(); i++) {
Person person = persons.get(i);
System.out.println(person.getFirstName());
int size = persons.size(); // Do with it whatever you want.
}
See also:
- How to check if there is zero-or-one result or one-or-more results and their size
how get length or size of Resultset in java
If you just want the number of rows in the ResultSet
, you can do :
int size= 0;
if (rs != null)
{
rs.beforeFirst();
rs.last();
size = rs.getRow();
}
How can I get size in bytes of a column from result set in java jdbc?
You can't derive this from the result set metadata. You can use DatabaseMetaData.getColumns(...)
, column CHAR_OCTET_LENGTH
, but this is possibly not populated for data types other than char
/varchar
.
Get the size of a ResultSet in Java for SQLite
You should map your result to an entity. Create a list of those entities, and then simply get the size of the list.
If you want the size and the size only, then simply make a query to return a count of the result of your original query.
How to get the number of rows from a JDBC ResultSet?
resultSet.last();
int size = resultSet.getRow();
resultSet.beforeFirst();
System.out.println("row size is" + size);
But will be better to use SELECT COUNT(*) FROM table
How to get row count using ResultSet in Java?
If you have access to the prepared statement that results in this resultset, you can use
connection.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
This prepares your statement in a way that you can rewind the cursor. This is also documented in the ResultSet Javadoc
In general, however, forwarding and rewinding cursors may be quite inefficient for large result sets. Another option in SQL Server would be to calculate the total number of rows directly in your SQL statement:
SELECT my_table.*, count(*) over () total_rows
FROM my_table
WHERE ...
Oracle SQL / Java : Get the size of returned dataset from a query
Docs:
A
ResultSet
object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
ResultSet
is not a collection, it is merely an abstraction of the cursor that is being used to get the data in a row-wise manner.
So, what exactly do need? The amount of memory needed to store the result? The size of the data in the database? ...? Why would it be nice?
You can always do SELECT COUNT(*) FROM
and using a certain average size of row estimate the result size... Instead of using the SELECT COUNT(*)
you can use a more convoluted way: go to the last element ResultSet.last()
and get the row number: ResultSet.getRow()
.
Related Topics
How to Fix Role in Spring Security
Processbuilder: Forwarding Stdout and Stderr of Started Processes Without Blocking the Main Thread
Convert Latitude/Longitude Point to a Pixels (X,Y) on Mercator Projection
Importing Two Classes with Same Name. How to Handle
Build Eclipse Java Project from Command Line
Deploying Spring 5.X on Tomcat 10.X
Collections.Sort with Multiple Fields
How to Use a Wildcard in the Classpath to Add Multiple Jars
Parse JSON from Httpurlconnection Object
Certain Unix Commands Fail with "... Not Found", When Executed Through Java Using Jsch
Why Stringbuilder When There Is String
Why Is "Extends T" Allowed But Not "Implements T"
Java Random Numbers Using a Seed
What Is the Use of Interface Constants
Java Filewriter with Append Mode