Several ways to get the number of records to RS by resultset

Source: Internet
Author: User
Tags rowcount

The ResultSet API in JDBC does not have a way to get the number of records directly, and now describes several:

Method one: Using ResultSet's GetRow method to get the total number of resultset

ResultSet rs = ps.executequery (SQL);

Rs.last (); Move to the last row

int rowCount = Rs.getrow ();//Gets the current line number, which is the number of records

Rs.beforefirst ();

If you want to use the result set again, move the pointer to the initialized position

Method Two: Using the elements of cyclic resultset to get the total number of resultset

ResultSet rs = ps.executequery (SQL);

int rowCount = 0; while (Rs.next ())

{

rowcount++;

}

Method Three: Use the Count function in the SQL statement to get the total number of resultset

String sql = "SELECT COUNT (*) record from tablename";

ResultSet rs = ps.executequery (SQL);

int rowCount = 0;

if (Rs.next ())

{

Rowcount=rs.getint ("Record");

}

It is true that the JDK API does not give us a very explicit number of records on the resultset result set, but we still know through the method that there are other ways to work around this. So, we often use the JDK, perhaps you want a feature, although in the API may not find the corresponding method, but often can be implemented in other ways, although sometimes such a workaround may not be efficient, But it is also often our own to achieve more than it may be more than a variety of conversion to better, so must learn more, more research API, whether it is the JDK, or other technology, in fact, as long as you are built on its platform, your vast majority of the needs can be through its existing to sublimate. Anyway is to have more than one kind of thinking, is standing on the shoulders of others to learn and sublimation, I believe that we use the JDK, open source framework and Toolkit development level is very high.

Several ways to get the number of records to RS by resultset

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.