[text]
Overview ResultSet contains all rows that meet the conditions in the SQL statement, and it provides access to data from these rows through a GET method (these GET methods can access different columns in the current row). The ResultSet.next method is used to move to the next row in the ResultSet, making the next row as the current line. The result set is generally a table, which has the column header and the corresponding value returned. For example, if the query is SELECT A, B, C from Table1, the result set will have the following form:
A B C ------------------------ 12345 Cupertino CA83472 Redmond WA83492 Boston MA The following code segment is an example of executing SQL statements. The SQL statement will return a set of rows, where column 1 is int, column 2 is String, and column 3 is byte arrays:
Java.sql.Statement Stmt = conn.createstatement (); ResultSet R = Stmt.executeQuery ("SELECT A, B, C from Table1"); while (r.next ()) {// Print the value of the current row. INT i = r.Getint ("a"); string s = r.getstring ("b"); float f = r.Getfloat ("c"); system.out.println ("row =" i " " S " " f);} 1, rows and cursor ResultSet Maintenance points to its current data line cursor. Each time the next method is called, the cursor moves down. Initially, it was located before the first line, so the first call next time, the cursor will be placed on the first line, making it a current line. As the NEXT that causes the NEXT to cause the cursor to move down, obtain the Resultset line according to the order from the top. The cursor remains valid until the ResultSet object or its parent Statement object is turned off. In SQL, the cursor of the result table is named. If the database allows positioning updates or locating deletion, you need to provide the name of the cursor as a parameter to the update or delete command. The light scalar can be obtained by calling method getcursorname. Note: Not all DBMS supports positioning updates and deletion. You can use the DatabaseMetadata.supportsPositionedDelete and SupportSpositionedUpDate methods to check if specific connections support these operations. When these operations are supported, the DBMS / driver must ensure that the selected line is properly locked so that the positioning update does not cause the update of exceptions or other concurrency issues.
2, the column method getxxx provides a way to get a certain value in the current row. In each line, you can get column values in any order. However, in order to ensure portability, the column value should be obtained from left to right and read the column value at once. Column names or column numbers can be used to identify columns to get data from it. For example, if the RSET object RS is named "Title", and the value is stored as a string, then any of the following code will get the value stored in the column:
String s = rs.getstring ("title"); string s = rs.getstring (2); Note Column is numbered from left to right and starting from column 1. At the same time, the column name used as the input of the getxxx method is not case sensitive. The purpose of providing the list name is to allow users who specify the column name in the query to use the same name as the parameters of the getxxx method. On the other hand, if the SELECT statement does not specify a column name (eg, when it is exported in "select * from table1"), the column number should be used. In these cases, the household will not know the column name. In some cases, there may be multiple columns with the same name in the result set of SQL queries. If the column name is used as a parameter of the getxxx method, Getxxx will return the value of the first match column name. Thus, if multiple columns have the same name, you need to use the column index to ensure that the correct column value is retrieved. At this time, the use of the rating efficiency is slightly higher. About the information in the resultSet can be obtained by calling the method ResultSet.getMetadata. The returned ResultSetMetadata object will give the number, type, and properties of each column of its ResultSet object. If the column name is known, but do not know the index, you can use the method FINDCOLUMN to get its column number. 3, data type and conversion For the getxxx method, the JDBC driver attempts to convert the basic data into the specified Java type and then return the suitable Java value. For example, if the getxxx method is getString, and the data type in the basic database is VARCHAR, the JDBC driver will convert VARCHAR to Java String. GetString's return value will be a Java String object. The following table shows the JDBC type that allows GetXXX and the JDBC type (universal SQL type) that is recommended for getting it. The lowercase x represents the Getxxx method to get the data type; the upper-written X indicates that the Getxxx method is recommended for this data type. For example, in addition to any getXXX method other than getBytes and getBinaryStream can be used to get the longvarChar value, but it is recommended to use the Getasciistream or the getUnicODestream method based on the returned data type. Method GetObject returns any data type to Java Object. It is very useful when the basic data type is an abstract type specific to a database or when a universal application needs to accept any data types. You can use the ResultSet.Getxxx method to get a common JDBC data type. "X" indicates that the getxxx method can be legally used to obtain a given JDBC type. "X" means that it is recommended to use the Getxxx method to get a given JDBC type.
getByte X xxxxxxxxxxxx getShort x X xxxxxxxxxxx getInt xx X xxxxxxxxxx getLong xxx X xxxxxxxxx getFloat xxxx X xxxxxxxx getDouble xxxxx XX xxxxxx getBigDecimal xxxxxxx XX xxxx getBoolean xxxxxxxxx X xxx getString xxxxxxxxxx XX xxxxxxx getBytes XX x getDate xxx X x getTime xxx X x getTimestamp xxx x X getAsciiStream xx X xxx getUnicodeStream xx X xxx getBinaryStream xx X getObject xxxxxxxxxxxxxxxxxx x4, for very large values of the row can be acquired using a flow ResultSet arbitrarily large or LONGVARCHAR LONGVARBINARY data. Methods getBytes and getString return data to large blocks (maximum return value for Statement.getMaxFieldSize). However, it may be more convenient to get very large data with a smaller fixed block, and this can be done by returning the ResultSet class to java.Io.input. The data can be read from the stream. Note: These streams must be accessed immediately because they will automatically turn off when the result is called getxxx next time (this is due to the basic implementation of large block data access). The JDBC API has three ways to get flow, each with different return values: GetBINARYSTREAM returns only the stream that only provides database origin without any conversion. GetasciistReam returns a stream that provides a single-byte ASCII character. GetUnicODestream returns a stream that provides a double-byte Unicode character. Note: It is different from the Java stream, and the latter returns non-type bytes and can be (for example, universal in ASCII and Unicode characters. The following code demonstrates the usage of getasciistream: