Original link address this article does not give a specific example of how to use resultset, but only from the resultset of the functional features of the detailed narration. I hope this article will help you understand resultset. The following is the specific content of this article.
Result set (ResultSet) is an object returned by query result in data, it can be said that the result set is an object to store query results, but the result set does not only have the function of storage, but also has the function of manipulating the data, possibly completing the update of the data, etc. int colindex/string colname) to get the ASCII stream of the current row for the column. That is to say, all getxxx methods operate on the current row. The reason is that the most basic resultset is because, this resultset his role is to complete the query results of the storage function, and can only read one time, can not scroll back and forth to read. This result set is created in the following ways:
ResultSet rs = statement.excutequery (SQLSTR);
This type supports scrolling back and forth to record next (), previous (), to the first row (), and also supports the resultset of the row absolute (int n) to be moved to the current row relative (int n) , to implement such resultset when creating statement, use the following method.
ResultSet rs = st.executequery (SQLSTR)
ResultsetType is to set the type of ResultSet object to scroll or not to scroll. The values are as follows: Resultset.type_forward_only can only be scrolled forward resultset.type_scroll_insensitive and result.type_scroll_sensitive both methods are It is possible to implement any forward and backward scrolling, using a variety of moving resultset pointer methods. The difference is that the former is insensitive to modification, and the latter is sensitive to modification. Resultsetconcurency is a set of ResultSet objects that can be modified, as follows: Resultset.concur_read_only is set as a parameter of a read-only type. Resultset.concur_updatable is set to a parameter of a modifiable type. So if you just want to be able to scroll the type of result, just assign the statement to the following values. RESULTSET.CONCUR_READ_ONLY);
ResultSet rs = st.excutequery (SQLSTR);
This ResultSet object can be used to modify the tables in the database, but I know that resultset just corresponds to a view of the tables in the database, so all resultset can be updated as soon as they are set to be updatable. ResultSet SQL statements that can complete the update must have the following attributes: A, only a single table is referenced. b, does not contain a join or a GROUP BY clause. C, those columns to include the primary key. With the above conditions, updatable resultset can complete modifications to the data, and the updatable result set is created by:
Statement st = createstatement (result.type_scroll_insensitive,result.concur_updatable)
The result of such statement is an updatable result set. The Update method is to move the resultset cursor to the row you want to update, and then call Updatexxx (), which means XXX has the same meaning as getxxx (). The Updatexxx () method has two parameters, the first is the column to be updated, either the column name or the ordinal number. The second is the data to be updated, which is the same data type as XXX. Each completed update to a row calls Updaterow () to complete the write to the database, and the modification will not be committed until the resultset cursor has not left the modified row. You can also complete the insert operation using the Updatexxx method. But first, there are two methods: Movetoinsertrow () is to move the resultset to the Insert row, this insert row is a special row in the table, do not need to specify the specific line, as long as the call to this method system will automatically move to that line. Movetocurrentrow () This is a line that moves resultset to memory, usually the current line. If you do not use the insert operation, this method has no effect, if you use the insert operation, this method is used to return to the line before the insert operation, leaving the insert row, of course, you can also through the next (), previous () and other methods to leave the insert row. To complete the insert of the database, first invoke Movetoinsertrow () to move to the insert row, and then call the Updatexxx method to complete the update of the column data, complete the update and update the operation, to write to the database. However, the InsertRow () is used here, and the ResultSet does not leave the Insert column before the method is executed, otherwise the insert is not executed, and the update to the inserted row is lost. Normally, if you use statement to perform a query and then execute another query, the result set of the first query is closed, that is, all statement queries have a result set that is one if you call connection's commit () Method also closes the result set. Retention is when the result of a resultset is submitted, is closed or not closed. JDBC2.0 and 1.0 are both submitted and the ResultSet will be closed. However, in JDBC3.0, we can set whether the resultset is closed. To complete the creation of such ResultSet objects, the creation of the statement to be used has three parameters, the StatemThe way ENT is created is what I call the third form of statement. As follows:
ResultSet rs = st.excutequery (SQLSTR);
Resultsetholdability indicates whether the result set is open after the result set is committed, with two values: Resultset.hold_cursors_over_commit: Indicates that the database is not closed when a commit is modified. Resultset.close_cursors_at_commit: Indicates that ResultSet is closed when a modification is committed. The ResultSet interface provides access to the data table. ResultSet objects are usually generated by executing "statements." table 102. ResultSet interface Field 102. ResultSet interface Field
field Type |
Field |
static int |
concur_read_only This constant indicates the parallelism of ResultSet objects that cannot be updated. Note : DB2 everyplace does not support concur_updatable. If you specify concur_updatable for the parallelism of the ResultSet object when you create the statement object, the DB2 everyplace JDBC driver emits a sqlwarning on the connection object that produces the statement object and uses Concur_ Read_Only instead. |
static int |
type_forward_only This constant indicates the type of ResultSet object whose cursor can only be moved forward. |
static int |
type_scroll_insensitive This constant indicates the type of ResultSet object that can be scrolled but is usually not sensitive to changes made to others. Note : Do not use this type of ResultSet object frequently because it may affect performance. This type uses sql_insensitive as the value of the CLI statement property sql_attr_cursor_sensitivity. For more information, see the documentation for the CLI function sqlsetstmtattr. |
static int |
type_scroll_sensitive This constant indicates the type of ResultSet object that can be scrolled and is usually sensitive to changes made to others. Note : This type uses sql_unspecified as the value of the CLI statement property sql_attr_cursor_sensitivity. For more information, see the documentation for the CLI function sqlsetstmtattr. |
Table 103. ResultSetInterface method
method return value type |
Method |
Boolean |
Absolute (int row) JDBC 2.0. Moves the cursor to a given line number in the result set. |
void |
Afterlast () JDBC 2.0. Moves the cursor to the end of the result set, just after the last line. |
void |
Beforefirst () JDBC 2.0. Move the cursor to the front of the result set, just before the first row. |
void |
clearwarnings () Clears all warnings reported on this ResultSet object. |
void |
Close () Release the database and JDBC resources for this ResultSet object immediately instead of waiting for the object to shut down automatically. |
Int |
Findcolumn (String ColumnName) maps a given ResultSet column name to its ResultSet column index. |
Boolean |
the () JDBC 2.0. Moves the cursor to the first row in the result set. |
BigDecimal |
Getbigdecimal (int columnindex) JDBC 2.0. Gets the value of a column in the current row in the form of a Java.math.BigDecimal object with full precision. The DB2 everyplace JDBC driver for Palm OS does not support this method. |
BigDecimal |
Getbigdecimal (int columnindex, int scale) in the Java programming language JAVA.MATH.BIGDECIM
|
BigDecimal |
Getbigdecimal (int columnindex, int scale) Gets the value of the specified column in the current row of this ResultSet object in the form of a Java.math.BigDecimal object in the Java programming language. The DB2 everyplace JDBC driver for Palm OS does not support this method. not supported. |
BigDecimal |
Getbigdecimal (String columnName) JDBC 2.0. Gets the value of a column in the current row in the form of a Java.math.BigDecimal object with full precision. The DB2 everyplace JDBC driver for Palm OS does not support this method. |
BigDecimal |
Getbigdecimal (String columnName, int scale) Gets the value of the specified column in the current row of this ResultSet object in the form of a Java.math.BigDecimal object in the Java programming language. The DB2 everyplace JDBC driver for Palm OS does not support this method. not supported. |
Blob |
GetBlob (int columnindex) JDBC 2.0. Gets the BLOB value in the current row of this ResultSet object. |
Blob |
GetBlob (String columnName) JDBC 2.0. Gets the BLOB value in the current row of this ResultSet object. |
Boolean |
Getboolean (int columnindex) Gets the value of a column in the current row as a Java boolean value. |
Boolean |
Getboolean (String columnName) Gets the value of a column in the current row as a Java boolean value. |
Byte |
GetByte (int columnindex) Gets the value of the specified column in the current row of this ResultSet object in bytes form in the Java programming language. |
Byte |
GetByte (String columnName) Gets the value of the specified column in the current row of this ResultSet object in the form of bytes in the Java programming language. |
Byte[] |
getBytes (int columnindex) Gets the value of the specified column in the current row of this ResultSet object in the form of a byte array in the Java programming language. |
Byte[] |
getBytes (String columnName) Gets the value of the specified column in the current row of this ResultSet object in the form of a byte array in the Java programming language. |
Int |
getconcurrency () JDBC 2.0. Returns the parallelism of the result set. |
Date |
getDate (int columnindex) Gets the value of the specified column in the current row of this ResultSet object in the form of a Java.sql.Date object in the Java programming language. |
Date |
getDate (int columnindex, Calendar cal) returns the value of the specified column in the current row of this ResultSet object in the form of a Java.sql.Date object in the Java programming language. |
Date |
getDate (String columnName) Gets the value of the specified column in the current row of this ResultSet object as a Java.sql.Date object in the Java programming language. |
Double |
getdouble (int columnindex) Gets the value of a column in the current row in Java double form. |
Double |
getdouble (String columnName) Gets the value of a column in the current row in Java double form. |
Float |
getfloat (int columnindex) Gets the value of a column in the current row in Java floating-point form. |
Float |
getfloat (String columnName) Gets the value of a column in the current row in Java floating-point form. |
Int |
getInt (int columnindex) Gets the value of the specified column in the current row of this ResultSet object in the form of an integer in the Java programming language. |
Int |
getInt (String columnName) Gets the value of the specified column in the current row of this ResultSet object in the form of an integer in the Java programming language. |
Long |
Getlong (int columnindex) Gets the value of a column in the current row in the form of a Java long integer. |
Long |
Getlong (String columnName) Gets the value of a column in the current row in the form of a Java long integer. |
ResultSetMetaData |
GetMetaData () Retrieves the number, type, and properties of the columns for this ResultSet object. |
Object |
GetObject (int columnindex) Gets the value of a column in the current row as a Java object. |
Object |
GetObject (String columnName) Gets the value of a column in the current row as a Java object. |
Int |
GetRow () JDBC 2.0. Retrieves the current line number. |
Short |
Getshort |
Short |
Getshort (String columnName) Gets the value of the specified column in the current row of this ResultSet object in short form in the Java programming language. |
Statement |
getstatement () JDBC 2.0. Returns the "statement" that generated this ResultSet object. |
String |
getString (int columnindex) Gets the value of the specified column in the current row of this ResultSet object in a String form in the Java programming language. |
String |
getString (String columnName) Gets the value of the specified column in the current row of this ResultSet object in a String form in the Java programming language. |
Time |
GetTime (int columnindex) Gets the value of the specified column in the current row of this ResultSet object in the form of a Java.sql.Time object in the Java programming language. |
Time |
GetTime (String columnName) Gets the value of the specified column in the current row of this ResultSet object as a Java.sql.Date object in the Java programming language. |
Timestamp |
Gettimestamp (String columnName) Gets the value of the specified column in the current row of this ResultSet object as a Java.sql.Timestamp object in the Java programming language. |
Timestamp |
Gettimestamp (int columnindex) Gets the value of the specified column in the current row of this ResultSet object in the form of a Java.sql.Timestamp object in the Java programming language. |
Int |
GetType () JDBC 2.0. Returns the type of this result set. |
SQLWarning |
getwarnings () returns the first warning of the call report on this ResultSet. |
Boolean |
Isafterlast () JDBC 2.0. Indicates whether the cursor is behind the last row in the result set. |
Boolean |
Isbeforefirst () JDBC 2.0. Indicates whether the cursor precedes the first row in the result set. |
Boolean |
Isfirst () JDBC 2.0. Indicates whether the cursor is on the first row in the result set. |
Boolean |
Islast () JDBC 2.0. Indicates whether the cursor is on the last row in the result set. This method is not supported for result sets with type type_forward_only. |
Boolean |
Last () JDBC 2.0. Moves the cursor to the last row in the result set. |
Boolean |
Next () moves the cursor down one row from its current position. |
Boolean |
previous () JDBC 2.0. Moves the cursor to the previous row in the result set. |
Boolean |
relative (int rows) JDBC 2.0. Moves the cursor relative to the number of rows, positive or negative. |
Boolean |
Wasnull () reports whether the last column read has a value of SQL NULL. |
A simple and useful new feature of ResultSet recordsets in JDBC API 2.0/3.0 for ResultSet recordsets in JDBC API 2.0/3.0
1 A number of new constants are definedThese constants are used to specify the nature of the direction of the type cursor movement of the ResultSet, as follows: public static