Detailed description of the Java resultset Interface

Source: Internet
Author: User
Detailed description of the Java resultset Interface

Record set Interface

In jdbc api 2.0, the resultset interface has changed a lot, adding a lot of new methods for Row Operations and row location, with strong functionality

The major changes are as follows:

1. Several new constants are defined.
These constants are used to specify the type of the resultset, the direction of the cursor movement, and other properties, as shown below:
Public static final int fetch_forward;
Public static final int fetch_reverse;
Public static final int fetch_unknown;
Public static final int type_forward_only;
Public static final int type_scroll_insensitive;
Public static final int type_scroll_sensitive;
Public static final int concur_read_only;
Public static final int concur_updatable;
Fetch_forward: This constant is used to specify that the order of rows in the processing record set is from the beginning to the end, that is, from the first row.

To the last line.
Fetch_reverse: This constant is used to specify that the order of rows in the record set is from back to front, that is, processing starts from the last row. I

Until the first line.
Fetch_unknown: This constant does not specify the order of rows in the processing record set, which is determined by the JDBC driver and the database system.
Type_forward_only: This constant is used to specify that the direction of moving the database cursor is forward, and backward movement is not allowed.

Use the next () method of the resultset interface instead of the previous () method. Otherwise, an error occurs.
Type_scroll_insensitive: This constant is used to specify that the database cursor can be moved before and after the record set, and the current data

The record set obtained by the database user is not sensitive to the operations of other users, that is, the current user is browsing the data in the record set. At the same time,

Other users update the data in the database, but the data in the record set obtained by the current user is not affected.
Type_scroll_sensitive: This constant is used to specify that the database cursor can be moved before and after the record set.

The record set obtained by the user is sensitive to the operations of other users, that is, the current user is browsing the record set, but the operations of other users are

The data in the database has changed, and the data in the record set obtained by the current user will also change synchronously.

A serious error occurs. We recommend that you use this constant with caution.
Concur_read_only: This constant is used to specify the concurrency mode of the current record set as read-only.

With this constant, the User Token cannot update the data in the record set.
Concur_updatable: This constant is used to specify the concurrency mode of the current record set to be updated.

Once this constant is used, you can use updatexxx () and other methods to update the data in the record set.
How can we use these predefined constants in JSP/servlet programs? This is a matter of great concern to readers. This article will be introduced in section 9.2.2.
.
2. The resultset interface provides a complete set of locating methods.
These can be located on any row in the record set, specifically:
Public Boolean absolute (INT row); this method is used to set a row in the record set to the current row, which is about to count

The database cursor moves to the specified row. The row parameter specifies the row number of the target row. This is an absolute row number, which is calculated from the first row of the record set,

It is not a relative row number.
Public Boolean relative (INT rows); this method is used to set a row in the record set to the current row, but it

The Rows parameter indicates the row number of the target row relative to the current row. For example, if the current row is 3rd rows, you need to move 6th rows now.

The absolute () method can also use the frelative () method. The Code is as follows.
Example:
Rs. Absolute (5 );
Or
Rs. Relative (2 );
RS represents the Instance Object of the resultset interface.
For example, if the current row is 5th rows, you need to move to 3rd rows. The Code is as follows.
Example:
Rs. Absolute (3 );
Or
Rs. Relative (-2 );
RS represents the Instance Object of the resultset interface.
The reader must note that the parameter passed to the relative () method, if it is a positive number, the database cursor moves forward, as shown in

If it is a negative number, the database cursor moves backward.
Note: In this document, the forward movement of the database cursor points to the increasing direction of the row number, and the backward movement points to the decreasing of the row number.

Method movement.
Public Boolean first (); this method is used to locate the current row to the first row of the database record set.
Public Boolean last (); this method is opposite to the first () method. It is the most effective method for locating the current row to the database record set.

The last line.
Public Boolean isfirst (); this method is used to check whether the first row of the current row records the set. If yes, true or no is returned.

Then, false is returned.
Public Boolean islast (); this method is used to check whether the last row of the current row records the set. If yes, true is returned,

Otherwise, false is returned.
Public void afterlast (); this method is used to move the database cursor to the end of the record set, located in the last row of the record set

If the record set does not contain any rows, this method does not apply.
Public void beforefirst (); this method moves the cursor of the database to the beginning of the record set, which is located at the first of the record set.

Before a row, if the record set does not contain any rows. This method does not work.
Public Boolean isafterlast (); this method checks whether the database cursor is at the end of the record set (that is, whether it is at the end of the record set)

Returns true if yes. Otherwise, returns false.
Public Boolean isbeforefirst (); this method checks whether the database cursor is at the beginning of the record set (that is, whether it is at the beginning)

Returns true if yes. Otherwise, returns false.
Public Boolean next (); this method is used to move the cursor of a database to the current row.

When a record set object is opened, the database cursor is located at the beginning of the record set. The first time you use the next () method, the database cursor will be

Locate the first row of the record set. The next () method will cause the database cursor to locate the second row of the record set, and so on.
NOTE: If an input stream is opened in the current row, the next () method will be disabled automatically.

The input stream.
Public Boolean previous (); this method is used to move the database cursor backward to make the previous row the current row.

3. Added support for row operations for the resultset interface.
With JDBC api2.0, you can not only randomly locate the database cursor to a specific row in the record set, but also use the resultset interface.

A newly defined method is used to update the data of the current row. In the past, if Java programmers wanted to update the data of a row in the record set, they had to send

SQL statements are provided to the database. Programmers need to embed lengthy SQL statements in Java code to execute update, delete, insert, and other data

Database Operations, but when JDBC api2.0 appeared, everything changed. programmers can leave SQL language aside and enjoy Java

Programming is fun. Some newly added methods in the resultset interface are as follows:
Public Boolean rowdeleted (); If a row in the current record set is deleted, a blank space will be set in the record set,

Call the rowdelete () method. If the existence of a vacant space is detected, true is returned. If no empty space exists, true is returned.

False.
Public Boolean rowinserted (); if a new row is inserted in the current record set, this method returns true; otherwise

False.
Public Boolean rowupdated (); returns true if the data of the current row in the current record set is updated; otherwise, returns

False.
Public void insertrow (); this method inserts a new row into the current record set;
Public void updaterow (); this method updates the data of the current row of the current record set.
Public void deleterow (); this method deletes the current row of the current record set.
Public void updatestring (INT columnindex, string X); this method updates the value of a column in the current row of the current record set.

The data type of a column is string (the Java data type is string, and the corresponding JDBC data type is varchar or nvarchar.

Type), The columnindex parameter of this method specifies the column index of the column to be updated, the first column index is 1, and so on, the second

Parameter X indicates the new value. This method does not perform database operations. It must be recorded after the insertrow () method or the updaterow () method.

And the data in the database.
Public void updatestring (string columnname, string X); this method is similar to the method with the same name described above,

However, the first parameter of this method is columnname, which indicates the column name of the column to be updated, rather than columnindex.
The resultset interface also defines many updatexxx () methods, which are similar to the above two methods. Due to the length,

I will not describe it in detail here. Readers interested in this can refer to the relevant literature.
The procedure for inserting a new row into the current record set of the database is as follows:
(1) Call the movetoinsertrow () method.
(2) Call the updatexxx () method to specify the values of each column in the inserted row.
(3) Call the insertrow () method to Insert a new row into the database.

4. Added support for the sql3 database type for the new resultset Interface
Several new data types are added to sql3 technical specifications, such as REF and array. The resultset interface expands the getxxx () method,

Added the getxxx () method for obtaining data of these types, such as getarray (), getblob (), getbigdecimal (), getclob (),

Getref () methods. These methods can receive both the column index as a parameter and the column name (field name) as a parameter.

Corresponding Java object instances, such as clob, array (JDBC array), blob, bigdecimal, and ref.

The usage of these methods will also be involved below, and we will not go into details here.

5. How to obtain the number of Record Sets
(1) first, use the last () method to locate the database cursor to the last row of the record set.
(2) Use the getrow () method to return the row index of the last row of the record set. This index is equal to the number of records contained in the record set.
Is the number of rows in the record set. The getrow () method is defined in jdbc api 2.0. This method is not available in jdbc api 1.0.
 

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.