JDBC obtains record data that meets query conditions-resultset

Source: Internet
Author: User
JDBC does not intuitively provide us with the number of records of the resultset query result. In this case, you cannot blindly call the method that looks like obtaining the number of records without querying the API. For example, getfetchsize () and getrow (), this will only fall into your own subjective trap. Let's take a look at the resultset API provided by JDK. Public interface resultsetextends wrapper indicates the data table of the database result set, which is usually generated by executing the database query statement. The resultset object has a cursor pointing to its current data row. Initially, the cursor is placed before the first line. The next method moves the cursor to the next row. Because this method returns false if the resultset object does not have the next row, it can be used in the while loop to iterate the result set. The default resultset object cannot be updated, and only one cursor moves forward. Therefore, it can only be iterated once, and can only be performed in the order from the first row to the last row. You can generate rolling and/or updatable resultset objects. Below Code The section (in which con is a valid connection object) demonstrates how to generate an updatable result set that can be rolled and is not affected by other updates. For other options, see the resultset field. Statement stmt = con. createstatement (resultset. type_scroll_insensitive, resultset. concur_updatable); resultset rs = stmt.exe cutequery ("select a, B from Table2"); // RS will be scrollable, will not show changes made by others, // and will be updatableresultset interface provides methods for getting column values (getboolean, getlong, etc.) from the current row ). You can use the column index number or column name to obtain the value. Generally, column indexes are more efficient. The column number starts from 1. To achieve maximum portability, you should read the result set columns in each row from left to right. Each column can only be read once. For obtaining methods, the JDBC driver Program Try to convert the underlying data to the Java type specified in the get method, and return the appropriate Java value. The JDBC specification has a table that displays the Mappings of Java types allowed to obtain methods from the SQL type to the resultset. The column names used for obtaining methods are case-insensitive. If multiple columns have this name, the value of the first matching column is returned. When column names are used in SQL queries that generate a result set, the column name option is used. Column numbers are recommended for columns not explicitly specified in the query. If the column name is used, the programmer should ensure that the name uniquely references the expected column, which can be determined by the SQL as clause.

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.