Categories
Java SQL Server

Counting how many rows were returned by a JDBC ResultSet

Ever wanted to check how many result rows your ResultSet actually returned?

Share

After executing a JDBC query, you often want to know if anything was returned and if so how many rows exist in the ResultSet. To do that, use this snippet of code:


ResultSet rs = st.exeuteQuery();
rs.last();
int rsRowCount = rs.getRow();
rs.beforeFirst();

The rsRowCount variable will have the answer.
Make sure that your Statement or PreparedStatenent are created using the three parameter version of the Connection object methods createStatement() and PrepareStatement() like:
Statement statement =
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

OR

PreparedStatement ps = conn.prepareStatement([SQL EXPRESSION],
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);

Look at
the JavaDoc entry for this method.

Share
Share