ResultSet Introduction

xiaoxiao2021-03-06  102

This article did not give specific examples of how to use RESULTSET, just a detailed story from the functionality of ResultSet. I hope this article can help you understand that ResultSet can help. Below is the specific content of this article.

ResultSet is an object returned in the data in the data. It can be said that the result set is an object that stores the query results, but the result set does not only have a stored function, and he also has the function of manipulating data, it may be completed Update to data, etc.

Results The method of reading data is mainly getxxx (), and his parameters can make the integer represent the quadch column (starting from 1), it can also be a column name. Returns the value of the corresponding XXX type. If the time null value is corresponding, XXX is an object's null value, if XXX is a digital type, such as FLOAT, returns 0, Boolean returns false. Using getString () can return all columns, but the returned is a string type. The types of XXX can represent: Basic data types such as integer (int), Boolean, float, float, bit type, Byte, including some special types, such as: Date Type (java.sql.date), time type (java.sql.time), timestamp type (java.sql.timestamp), large number (BigDecimal and Biginteger, etc.). You can also use GetArray (int COLINDEX / STRING COLUMNAME), an array of objects that consist of the elements of the colorex listed in the current line through this method. The ASCII stream of the current row corresponding to the column can be obtained using GetASciistream (int COLINDEX / STRING colname). That is to say, all Getxxx methods are operated on the current line.

The result set can be divided into four categories from the characteristics of its use. The characteristics of the four-class result sets are related to the creation of the Statement statement, because the result set is generated after execution through the statement statement, so it can be said, The result set has the characteristics, which is fully determined in Statement, and of course I said the following four features, including three types when STATEMENT is created. The first is the parameter type, and he corresponds to the basic ResultSet corresponding to the basic ResultSet below. The connection used in the code in the following code does not initialize it, and the variable CONN represents the object corresponding to the Connection. SQLSTR represents a response SQL statement.

1. The most basic resultSet. The reason is the most basic resultset because this ResultSet he played to complete the storage function of the query result, and can only read it once, the scroll reading cannot be retrieved. The creation of this result set is as follows: statement st = conn.createstatementResultset RS = Statement.excuteQuery (SQLSTR); Since this result set does not support, scrolling read function, if such a result set, you can only use it The NEXT () method inside is read one by one.

2, can scroll the ResultSet type. This type supports a record next andie and rear (), ready (), back to the first line first (), and also support the first line of RESULUTE (int N) in the RESULTSET, and move to the relative current line Several RELATIVE (INT N) To achieve such a RESULTSET in creating a Statement method. Statement St = Conn.createStatement (int resultSetType, int resultSetConcurrency) ResultSet RS = St.executeQuery (SQLSTR) The meaning of two parameters is: ResultSetType is the type of setting of the ResultSet object can scroll, or cannot scroll. The value is as follows: ResultSet.Type_forward_only can only scrolute the RESULTSET.TYPE_SCROLL_INSENSITITIVE and RESULT.TYPE_SCROLL_SENSITIVE These two methods can implement any front and rear scrolling, and the use of various mobile RESULTSET pointers. The difference between the two is that the former is not sensitive to modifications, and the latter is sensitive to modification. ResultSetConcurency is the setting of the ResultSet object to be modified, and the value is as follows: resultt.concur_read_only is set to the read-only type parameter. ResultSet.concur_UPDATABLE is set to modify the type of parameters. So if you just want to scroll, you will assign Statement as follows. Statement st = conn.createstatement (Result.Type_Scroll_insenitive, ResultSet.concur_read_only); ResultSet RS = St.ExcuteQuery (SQLSTR); Query statement executed with this Statement is the scrollable ResultSet. 3, can be updated ResultSet objects can complete the modifications to the tables in the database, but I know that the ResultSet is just a view of the table in the database, so when all the resultSt can complete the update, it is possible to complete the update. The SQL statement for completing the updated RESULTSET must have the following properties: a, only a single table is only referenced. B, does not contain Join or Group By clauses. C, the primary keywords are included in those columns. With the above conditions, the updateable resultSet can complete the modification of the data, the updateable result set creation method is:

Statement St = CreateStatement (Result.Type_Scroll_insensitive, Result.concur_Updata)

The result of such statement is the updatable result set. The update method is to move the RESULTSET's cursor to the row you want to update, and then call Updatexxx (), this method XXX meaning and getxxx () are the same. Updatexxx () method, there are two parameters, the first is to update the column, which can be column name or serial number. The second is to update the data, this data type is the same as XXX. Each time you complete the UPDATE to call Updaterow (), you will not be submitted before the RESULTSET's cursor does not leave the modifier. You can also complete the insert operation using the Updatexxx method. But the first thing to introduce two methods: MoveToInSertrow () is moving the resultSet to the insert line, this insertion line is a special line in the table, does not need to specify the specific line, as long as the method is called, it will automatically move to that line. MoveToccurrentrow () This is a line that moves RESULTSET to memory, usually the current line. If there is no use of INSERT operation, this method does not have an effect. If INSERT operation is used, this method is used to return to the line before INSERT operation, leaving the insert, and can of course leave insert by next (), previous (). Row. To complete the insertion of the database, first call MoveToInSertrow () to move to insert row, then call Updatexxx's method to complete the update, the updates, after the update is completed, to write to the database, but here is INSERTROW (), It is also necessary to ensure that the RESULTSET has not left the insert column before the method is executed, otherwise the insertion is not executed, and the update to the insert will be lost.

The result of such statement is the updatable result set. The update method is to move the RESULTSET's cursor to the row you want to update, and then call Updatexxx (), this method XXX meaning and getxxx () are the same. Updatexxx () method, there are two parameters, the first is to update the column, which can be column name or serial number. The second is to update the data, this data type is the same as XXX. Each time you complete the UPDATE to call Updaterow (), you will not be submitted before the RESULTSET's cursor does not leave the modifier. You can also complete the insert operation using the Updatexxx method. But the first thing to introduce two methods: MoveToInSertrow () is moving the resultSet to the insert line, this insertion line is a special line in the table, does not need to specify the specific line, as long as the method is called, it will automatically move to that line. MoveToccurrentrow () This is a line that moves RESULTSET to memory, usually the current line. If there is no use of INSERT operation, this method does not have an effect. If INSERT operation is used, this method is used to return to the line before INSERT operation, leaving the insert, and can of course leave insert by next (), previous (). Row. To complete the insertion of the database, first call MoveToInSertrow () to move to insert row, then call Updatexxx's method to complete the update, the updates, after the update is completed, to write to the database, but here is INSERTROW (), It is also necessary to ensure that the RESULTSET has not left the insert column before the method is executed, otherwise the insertion is not executed, and the update to the insert will be lost. 4, can be kept, if you use Statement to execute a query, when you perform another query, this time the first query will be closed, that is, all the STATEMENT results The set is one, if the COMMIT () method calling the connection will also turn off the result set. The maintenanceability is that when the result of the resultSet is submitted, it is closed or not closed. The JDBC2.0 and 1.0 is provided after submission, RESULTSET will be closed. However, in JDBC 3.0, we can set whether the resultset is turned off. To complete the creation of such a ResultSet object, the creation of Statement to be used To have three parameters, this Statement is created, the third creation of the Statement. as follows:

Statement st = createStatement (int resultsetscrollable, int resultsetupdateable, int resultsetSetHoldability) ResultSet rs = st.excuteQuery (sqlStr); createStatement first two methods and parameters in the two parameters are exactly the same parameters, the third parameter here only : ResultSetholdability indicates whether the result set is turned on after the result set is submitted, and there are two: resultSet.Hold_cursors_over_commit: Indicates that the database is not turned off. ResultSet.close_cursors_at_commit: ResultSet is turned off when the modification is submitted.

However, this feature is only the driver of JDBC3.0 can be established.

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

New Post(0)