Gets the number of resultset records

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
Java code ResultSet RS; Rs.last (); Move to the last line int rowCount = Rs.getrow (); Get 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

Possible exceptions: Java.sql.SQLException: Invalid operation for forward only result set: Last exception resolved

Beginners do not pay much attention to the establishment of objects, and later know that in the establishment of statement when the relevant parameters can be solved. The statement that an  
exception occurred during the operation of the query result set     
: Rs.last ();     
Exception Details: Invalid operation for forwarding only result sets: Last     
solution:   stat = conn.createstatement ();  
          Changed to Stmt=conn.createstatement (Resultset.type_scroll_insensitive,resultset.concur_read_only); It is possible to     
parse: An exception occurs when a pointer to a moving result set is generated because the parameter that is supplied when generating the statement object is  
using the default parameter, and the result set type obtained after statement execution is Resultset.type_forward_only. This type of result set can only be read by Rs.next (), Method-by-article, using other methods to report an exception. If you want to perform some complicated moving result set pointers, you will need to use other parameters. For a  
brief introduction of each parameter:  
resultset.type_forward_only    (slightly)  
resultset.type_ Scroll_insensitive   Two-way scrolling, but not in time to update, that is, if the data in the database has been modified, not in the resultset reaction.  
resultset.type_scroll_sensitive   Two-way scrolling and keeps track of updates in the database in order to change the data in the ResultSet.  
resultset.concur_read_only   only read ResultSet  
resultset.concur_updatable   Update database with ResultSet


Method Two: Using the elements of cyclic resultset to get the total number of resultset
Java code ResultSet RS;    int rowCount = 0;    while (Rset.next ()) {rowcount++; }

Method Three: Use the Count function in the SQL statement to get the total number of resultset
Java code String sql = "SELECT COUNT (*) Record_ from (SELECT * from yourtable t where T.column_ = ' value_ ')";    ResultSet rs = ps.executequery (SQL);    int rowCount = 0;    if (Rs.next ()) {Rowcount=rs.getint ("Record_"); }

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.