ResultSet result set

Source: Internet
Author: User
Tags stmt

All super interfaces: Autocloseable, wrapper
All sub-interfaces: CachedRowSet, Filteredrowset, Jdbcrowset, Joinrowset, RowSet, Syncresolver, Webrowset

The data in the database is converted to resultset in tabular form, usually by querying the database from a database query statement.
A resultset operation is achieved primarily by pointing to the current row data. By default, the pointer is before the first row of data (so to get the data you want, you first have to move the pointer to the corresponding row). The next () method moves the current pointer to the next row and returns False when the ResultSet object does not exist, so you can iterate through all the data in resultset in the loop.
The ResultSet object is immutable, and the pointer can only be "moved down." Therefore, you can only come one time at a time in an iteration, and only from the first line to the last line. Of course, it is possible to achieve a round-trip or resultset update. Some of the following code fragments are meant to show that resultset can be scrolled and slow for update operations, and that ResultSet is updatable, where con is a valid collection object,

Statement stmt = con.createstatement (
    resultset.type_scroll_insensitive,
    resultset.concur_updatable);
ResultSet rs = stmt.executequery ("Select a, b from TABLE2");
       RS can be scrolled and cannot be changed
       //pointers can change position

You can use the Getter () method in the current line (Getboolean (), Getlong (),... To retrieve the value of the column. And the value can be retrieved by the index of the column or by the property name of the column. However, it is more efficient to use the index of the column as a whole to retrieve it. The index of the column is starting from 1. To ensure maximum migration, columns in each row in the ResultSet should be read from left to right and read only once per line.
For the Getter () method, JDBC driver wants to convert the underlying data to Java-type data, allowing the appropriate getter () method and finally returning a value of the appropriate Java type. The JDBC driver specification shows the relevant data maps, which convert the data in SQL to Java-type data through the ResultSet getter method.

In the Getter () method, if you are entering a column name to get a value, the column name is not case-sensitive. If the getter () gets a duplicate of the column name of the value, the last value returned is the first successful match. The general use of column names is when generating resultset through a database query statement. Database queries do not have to use column names, or they can be indexed by columns. It is also best to use the index of the column. If you use a column name, you must guarantee that the column name in your program must correspond to column one by one, which you can use as a sentence in the database.

A series of updater () methods are added to JDBC 2.0. There are some assertions that parameters in the Getter () method can also be applied to parameters in the updater () method.
The updater () method can be implemented in the following two ways
1, update the value of the column in the current row.
In a ResultSet object that can be scrolled, the pointer moves back and forth, to a definite position, or to the position relative to the current position. The following code fragment functionality updates the column named name of line fifth of the ResultSet object RS, and then updates the Data Resource table for RS export using the Updaterow () method.

Rs.absolute (5); Move the pointer to line fifth
rs.updatestring ("name", "AINSWORTH") in RS;//update the row name to the Name column AINSWORTH
rs.updaterow ();//Update data resources for whole rows

2, insert the value inserted into the column and then inserted into the row.
Updatable ResultSet set objects have a special row that is related to the row that will be inserted. The following code fragment moves the pointer to the row that will be inserted, and then creates a row with three columns, inserting RS into the Data Resource table when the line is inserted into RS and then the same as the InsertRow () method.

Rs.movetoinsertrow (); Moves the pointer to the inserted row
rs.updatestring (1, "AINSWORTH");//change the column name of the first column of the row to AINSWORTH
rs.updateint (2,35);//Change the second column of the row to 35
Rs.updateboolean (3, true);//Change the third column of the row to True
Rs.insertrow ();
Rs.movetocurrentrow ();

The ResultSet object closes automatically when the statement object closes, is rerun, or is used to retrieve the next result set in a series of result sets. The number, type, and properties of the ResultSet object's columns are provided by the Resultset.getmetadata () method returned by the ResultSetMetaData object.

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.