JDBC ResultSet analysis (reproduced)

The result set (ResultSet) is an object returned by the query results in the data. It can be said that the result set is an object that stores the results of the query, but the result set not only has the function of storing, it also has the function of manipulating the data, which may be completed Updates to data, etc.

The main method to read data in the result set is getXXX(), whose parameters can be integers to indicate the number of columns (starting from 1), or column names. What is returned is the corresponding value of type XXX. If it corresponds to the null value in that column, if XXX is an object, it returns a null value of type XXX. If XXX is a numeric type, such as Float, it returns 0, and boolean returns false.

Use getString() to return the values ​​of all columns, but all returned are of string type. The types that XXX can represent are: basic data types such as integer (int), Boolean (Boolean), floating point (Float, Double), etc., bit type (byte), and some special types, such as: date Type (java.sql.Date), time type (java.sql.Time), timestamp type (java.sql.Timestamp), large number type (BigDecimal, BigInteger, etc.), etc.

You can also use getArray(int colindex/String columnname) to get the array of objects in the current row, the column where the column is located.

Use getAsciiStream (int colindex/String colname) to get the ascii stream of the current row corresponding to the column. In other words, all getXXX methods operate on the current row.

The result set can be divided into four categories from the characteristics of its use. The characteristics of these four types of result sets are related to the creation of Statement statements. Because the result sets are generated after the Statement statement is executed, it can be said that, The characteristics of the result set are completely determined by the Statement. Of course, I mean the following four characteristics, including three types when the Statement is created. The first is the parameterless type, which corresponds to the Statement corresponding to the basic ResultSet described below. The Connection used in the following code is not initialized, and the variable conn represents the object corresponding to the Connection. SqlStr represents the response SQL statement.

1. The most basic ResultSet.
The reason why it is the most basic ResultSet is because the function of this ResultSet is to complete the storage function of query results, and can only be read once, and cannot be scrolled back and forth. The way to create this result set is as follows:

Statement st = conn.CreateStatement
ResultSet rs = Statement.excuteQuery(sqlStr);

Since this result set does not support the scrolling read function, if you get such a result set, you can only use the next() method in it to read the data one by one.

2. Scrollable ResultSet type.
This type supports scrolling back and forth to obtain records next(), previous(), returning to the first line first(), and also supports absolute (int n) in the first few lines of the ResultSet to be moved, and moving to the first line relative to the current line A few lines of relative(int n), to achieve such a ResultSet, use the following method when creating a Statement.

Statement st = conn. createStatement (int resultSetType, int resultSetConcurrency)
ResultSet rs = st.executeQuery(sqlStr)

The meaning of the two parameters is:
  resultSetType is to set the type of the ResultSet object to be scrollable or non-scrollable. The values ​​are as follows:
    ResultSet.TYPE_FORWARD_ONLY can only scroll forward
    ResultSet.TYPE_SCROLL_INSENSITIVE and Result.TYPE_SCROLL_SENSITIVE These two methods can achieve arbitrary forward and backward scrolling, using various methods of moving the ResultSet pointer. The difference between the two is that the former is not sensitive to modification, while the latter is sensitive to modification.

resultSetConcurency can be modified by setting the ResultSet object, and the values ​​are as follows:
    ResultSet.CONCUR_READ_ONLY is set as a read-only parameter.
    ResultSet.CONCUR_UPDATABLE is set to a parameter of a modifiable type.
  So if you just want a scrollable type of Result, just assign Statement as follows.

Statement st = conn.createStatement(Result.TYPE_SCROLL_INSENITIVE,
ResultSet.CONCUR_READ_ONLY);
  ResultSet rs = st.excuteQuery(sqlStr) ;

The query statement executed with this Statement is a scrollable ResultSet.

3. Updatable
  ResultSet objects such as ResultSet can complete the modification of tables in the database, but I know that ResultSet is just a view of the tables in the database, so from time to time all ResultSets can be updated as long as they are set up to be updated. The SQL statement of the updated ResultSet must have the following attributes:
a. Only a single table is referenced.
b. Does not contain join or group by clauses.
c. Those columns must contain primary keywords.
With the above conditions, the updateable ResultSet can complete the modification of the data. The method for creating the updateable result set is:

Statement st = createstatement(Result.TYPE_SCROLL_INSENSITIVE,Result.CONCUR_UPDATABLE)

4. Retainable ResultSet.
  Under normal circumstances, if you use Statement to execute a query and then execute another query, the result set of the first query will be closed at this time, that is, the results of all Statement queries The set is one. If you call the commit() method of Connection, the result set will also be closed. Retentivity refers to whether the result of the ResultSet is closed or not when it is submitted. JDBC2.0 and 1.0 provide that the ResultSet will be closed after submission. But in JDBC3.0, we can set whether the ResultSet is closed. To complete the creation of such a ResultSet object, the creation of the Statement to be used must have three parameters. The method of creating this Statement is the third method of creating the Statement that I call.

When using ResultSet, when there are many records in the data set that are queried, 10 million, will the object pointed to by rs occupy a lot of memory? If there are too many records, will the program use the system memory What about the light?

No, the ResultSet looks like a record set on the surface. In fact, this object only records the relevant information of the result set. The specific record is not stored in the object. The specific record content knows when you extract it through the next method. The content of the fields can be obtained from the database when the related getXXXXX method is used to extract the field content. These do not occupy memory. The specific memory consumption is because you extract the data in the record set and add it to your own collection. If If you don't use the collection to record all the records, there will be no memory consumption.

Guess you like

Origin blog.csdn.net/weixin_45729934/article/details/105990479