Java program for getting ResultSet record rows and columns

Source: Internet
Author: User
Tags rowcount stmt

Core code for getting the number of record columns in the ResultSet

 

The code is as follows: Copy code
ResultSetMetaData rsmd = this. rs. getMetaData ();
This. columnCount = rsmd. getColumnCount ();

The code for java to obtain the number of data rows is as follows:

The code is as follows: Copy code
Statement stmt = conn. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_READ_ONLY );
ResultSet rs = stmt.exe cuteQuery (SQL );
Rs. last ();
Int length = rs. getRow ();

The value of length is the number of rows. If you want to continue using the current dataset rs after obtaining the number of rows, run rs. beforeFirst () to return the cursor to the initial position.

The common format created by Statement is:

The code is as follows: Copy code
Statement stmt = con. createStatement (int type, int concurrency );

Instance 1. Use the select count statement and obtain the result from ResultSet:

The code is as follows: Copy code
Try {
Statement statement = connection. createStatement ();
ResultSet resultSet = statement.exe cuteQuery ("select count (*) as rowCount from tableName ");
ResultSet. next ();
Int rowCount = resultSet. getInt ("rowCount ");
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}

      
Instance 2. Traverse the Resultset and use a variable to record the number of rows. The code is as follows:

The code is as follows: Copy code

Int count = 0;
Try {
While (resultSet. next ()){
Count = count + 1;
      }
} Catch (SQLException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
  }

Instance 3

The code is as follows: Copy code
Statement stmt = conn. createStatement (ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet. CONCUR_READ_ONLY );
ResultSet rs = stmt.exe cuteQuery (SQL );
Rs. last ();
Int length = rs. getRow ();

The value of length is the number of rows.

If you want to continue using the current dataset rs after obtaining the number of rows, run rs. beforeFirst () to return the cursor to the initial position.

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.