Java resultset get Total rows

Source: Internet
Author: User
Tags rowcount

In Java, there are several ways to get the total number of rows in resultset.

First: Use ResultSet's GetRow method to get the total number of resultset

Statement stmt = con.createstatement (resultset.type_scroll_insensitive,resultset.concur_updatable);
ResultSet RSet = Stmt.executequery ("SELECT * from Yourtablename");
Rset.last ();
int rowCount = Rset.getrow (); Get total rows for resultset

The second type: Use the elements of the loop resultset to get the total number of resultset

ResultSet RSet = Stmt.executequery ("SELECT * from Yourtablename");
int rowCount = 0;
while (Rset.next ()) {
rowcount++;
}

RowCount is the total number of resultset.

Third: Use the Count function in the SQL statement to get the total number of rows in resultset

ResultSet RSet = Stmt.executequery ("SELECT count (*) TotalCount from Yourtablename");
int rowCount = 0;
if (Rset.next ()) {
Rowcount=rset. GetInt ("TotalCount");
}

RowCount is the total number of resultset.

Fourth: The total number of columns in Java that gets resultset is a very simple thing, because Java ResultSet provides ResultSetMetaData tool classes, ResultSetMetaData is a collection of resultset metadata descriptions.

Statement stmt = con.createstatement (resultset.type_scroll_insensitive,resultset.concur_updatable);
ResultSet RSet = Stmt.executequery ("SELECT * from yourtable");
ResultSetMetaData RSMD = Rset.getmetadata ();
int columnCount = Rsmd.getcolumncount ();

Both: ColumnCount is the total number of columns of resultset.

Java resultset get Total rows

Related Article

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.