Obtain the number of records of the resultset in JDBC

Source: Internet
Author: User
Tags rowcount
The resultset API in JDBC does not directly obtain the number of records. Here we will introduce several methods: Method 1: Use the getrow method of resultset to obtain the total number of rows of resultset. Java code: resultset RS; RS. last (); // move to the last row int rowcount = Rs. getrow (); // obtain the current row number, that is, the number of records Rs. beforefirst (); // if you want to use a result set, move the pointer to the initialization position. Method 2: Use the element of the cyclic resultset to obtain the total number of rows of the resultset. Java code: resultset RS; int rowcount = 0; while (rset. next () {rowcount ++;} method 3: Use the count function in the SQL statement to obtain the total number of rows in the resultset. Java code string SQL = "select count (*) record _ from (select * From yourtable t where T. column _ = 'value _ ') "; resultset rs = ps.exe cutequery (SQL); int rowcount = 0; If (RS. next () {rowcount = Rs. getint ("record _");}

Resultset rs = stmt.exe cutequery ("select count (*) from table"); RS. Next (); int I = Rs. getint (1 );

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.