How to obtain the number of records in the resultset

Source: Internet
Author: User

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

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:
Statement stmt = con. createstatement (INT type, int concurrency );

Parameter description:

Int type

The cursor of the resultset. type_forword_only result set can only scroll down.
The cursor of the resultset. type_scroll_insensitive result set can be moved up or down. When the database changes, the current result set remains unchanged.
Resultset. type_scroll_sensitive returns a scrollable result set. When the database changes, the current result set changes synchronously.

Int concurrency

Resultset. concur_read_only cannot use the result set to update Tables in the database.
Resultset. concur_updatetable can use the result set to update Tables in the database.

When you use resultset re=stmt.exe cutequery (SQL statement) to query, you can use the following methods to obtain information:

Public Boolean previous () moves the cursor up. This method returns boolean data. If it is moved to the first row of the result set, false is returned.
Public void beforefirst moves the cursor to the initial position of the result set, that is, before the first row.
Public void afterlast () moves the cursor after the last row of the result set.
Public void first () moves the cursor to the first row of the result set.
Public void last () moves the cursor to the last row of the result set.
Public Boolean isafterlast () determines whether the cursor is behind the last row.
Public Boolean isbeforefirst () determines whether the cursor is before the first row.
Public Boolean iffirst () determines whether the cursor points to the first row of the result set.
Public Boolean islast () determines whether the cursor points to the last row of the result set.
Public int getrow () gets the row number pointed to by the current cursor. The row number starts from 1. If no row exists in the result set, 0 is returned.
Public Boolean absolute (INT row) moves the cursor to the row specified by the row parameter. If the row is negative, it is the number of rows to be counted. Absolute (-1) indicates moving to the last row, while absolute (-2) indicates moving to the last 2nd rows. When moving to the front of the first line or the end of the last line, this method returns false.

 

Getfetchsize
resultset.getFetchSize()

Returns the fetch size for the result set object.

 

Setfetchsize
resultset.setFetchSize(int rows)

Gives the driver an idea of how many rows shoshould be returned from the database when more rows are needed for the result set.

 

 

Getmetadata

resultset.getMetaData()

Returns a resultsetmetadata object with the number, type, and properties of the result set's columns.

Getrow

resultset.getRow()

Returns an integer containing the current row number.

 

Http://book.opensourceproject.org.cn/lamp/mysql/mastermysql/opensource/4966final/lib0185.html

 

If it is defined as follows:
Statement statement = con. createstatement ();
The resulting Rs only has the next () Attribute and does not support the first (), last (), or absolute () attributes.

 

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.