JDBC Overview (5)

zhaozj2021-02-16  73

5 - ResultSet 5.1 Overview The resultset contains all rows that meet the conditions in the SQL statement, and it provides access to data in 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: AB C ------------------------ 12345 CUPERTINO CA83472 Redmond WA83492 Boston MA The following code segment is an example of executing a SQL statement. The SQL statement will return a row collection, 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 ()) {// prints 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);} 5.1.1 Rows and cursor ResultSet Maintenance points to the cursor of its current data line. 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. 5.1.2 Column Method Getxx provides a way to get a column 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 second column name of the ResultSet object RS is "Title", then stores the value as a string, then the following code will get the value stored in the column: String s = rs.getstring ("Title") String s = rs.getstring (2); Note The column is numbered from left to right and starts 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. 5.1.3 Data Types and Conversion For the Getxxx method, the JDBC driver attempts to convert basic data to specify the Java type and then return the appropriate 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 xxxxxx xgetBytes XX x getDate xxx X xgetTime xxx X xgetTimestamp xxx x XgetAsciiStream xx X xxx getUnicodeStream xx X xxx getBinaryStream xx X getObject xxxxxxxxxxxxxxxxxx x5.1.4 using very large flow ResultSet row values ​​may be obtained in any 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.

转载请注明原文地址:https://www.9cbs.com/read-15828.html

New Post(0)