How does Java obtain the number of rows and columns in the SQL query result set?

Source: Internet
Author: User
Tags rowcount

Http://cheneyph.iteye.com/blog/477829

In Java, there are several methods to obtain the total number of rows of the resultset.

First, use the getrow method of the resultset to obtain the total number of rows of the resultset.

Statement stmt = con. createstatement (resultset. type_scroll_insensitive, resultset. concur_updatable );
Resultset rset = stmt.exe cutequery ("select * From yourtablename ");
Rset. Last ();
Int rowcount = rset. getrow (); // obtain the total number of rows in the resultset.

Type 2: Use the element of the cyclic resultset to obtain the total number of rows of the resultset.

Resultset rset = stmt.exe cutequery ("select * From yourtablename ");
Int rowcount = 0;
While (rset. Next ()){
Rowcount ++;
}

Rowcount is the total number of rows in the resultset.

Third: Use the count function in the SQL statement to obtain the total number of rows in the resultset.

Resultset rset = stmt.exe cutequery ("select count (*) totalcount from yourtablename ");
Int rowcount = 0;
If (rset. Next ()){
Rowcount = rset. getint ("totalcount ");
}

Rowcount is the total number of rows in the resultset.

·*************************************** **************************************** **********************************

· It is very simple to obtain the total number of columns of the resultset in Java, because the resultset provides the resultsetmetadata tool class, And resultsetmetadata is a set description of the metadata of the resultset.

Java obtains the total number of columns of the resultsetCodeAs follows:

Statement stmt = con. createstatement (resultset. type_scroll_insensitive, resultset. concur_updatable );
Resultset rset = stmt.exe cutequery ("select * From yourtable ");
Resultsetmetadata rsmd = rset. getmetadata ();
Int columncount = rsmd. getcolumncount ();

Columncount is the total number of columns in the resultset.

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.