Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Mar 9, 2004

Retrieve the rowCount Without Looping

Use this code to obtain the rowcount in JDBC without looping through the resultset.

    try
    {
      // Create a scrollable result set.
      stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,
                                   ResultSet.CONCUR_READ_ONLY);
      // Set a string with the query
      sqlString = "SELECT .....";

      rs = stmt.executeQuery(sqlString);

      // Point to the last row in resultset.
      rs.last();

      // Get the row position which is also the number of rows in the
resultset.
      int rowcount = rs.getRow();

      // Reposition at the beginning of the ResultSet to take up rs.next()
call.
      rs.beforeFirst();
      ...
      ....

    }
    catch(..)
    {
	..
    }
Shyamala Krishnan
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap