How to use Java database programming ResultSet

Source: Internet
Author: User

The result set (ResultSet) is an object returned by the query results in the data, which can be said that the result set is an object that stores the results of the query, but the result set does not only have the function of storage, but also has the function of manipulating the data, possibly completing the updating of the data, etc.

The result set reads the data mainly by GetXXX (), whose parameters allow the integer to represent the first column (starting at 1) or the column name . Returns the value of the corresponding XXX type.

If the time-space value corresponding to the column, XXX is the object of the return of the XXX type of NULL, if XXX is a numeric type, such as float and so on return 0,boolean return false.

You can use GetString () to return all the values of a column, but the return is of type string.

The types of XXX that can be represented are:

Basic data types such as Integer (int), Boolean (Boolean), floating-point (float,double), and so on, bit-type (byte),

Also includes some special types, such as: Date type (java.sql.Date), Time Type (java.sql.Time), timestamp type (java.sql.Timestamp),

Large number type (BigDecimal and BigInteger, etc.).

You can also use GetArray (int colindex/string columnname) to get an array of objects that consist of the elements of the column in the current row, Colindex.

Use Getasciistream (int colindex/string colname) to get the ASCII stream of the current row for that column. This means that all getxxx methods operate on the current line.


The result set from its use can be divided into four categories, the result set of the four class is related to the creation of the statement statement, because the result set is executed through the statement statement, so it can be said that the result set has what characteristics, fully determined by statement, Of course, I mean the following four features, including three types when statement is created. First, there is no parameter type, he corresponds to the following is the basic resultset corresponding to the statement. The connection used in the following code does not initialize it, and the variable conn represents the object that the connection corresponds to. The SQLSTR represents the SQL statement of the response.

1, the most basic resultset.
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 be read once, not be able to scroll back and forth to read. This result set is created in the following way:

Statement ST = Conn. Createstatement
ResultSet rs = statement.excutequery (SQLSTR);

Because this result set is not supported, scrolling read-to-go function so, if you get such a result set, you can only use the next () method inside it to read the data one by one.

2, scrollable resultset type.
This type supports front and rear scrolling to get the record next (), previous (), Back to first (), and also supports the number of rows in the ResultSet to go absolute (int n), and moves to the row of the relative current row relative (int n) , to implement such a resultset when creating statement, use the following method.

Statement ST = Conn. createstatement (int resultsettype, int resultsetconcurrency)
ResultSet rs = st.executequery (SQLSTR)

The meanings of two of these parameters are:
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
Both the resultset.type_scroll_insensitive and Result.type_scroll_sensitive methods are able to implement arbitrary forward and backward scrolling, using a variety of moving ResultSet pointers. The difference is that the former is not sensitive to modification, while the latter is sensitive to modification.
Resultsetconcurency is set ResultSet object can be modified, the value is as follows:
Resultset.concur_read_only parameters that are set to read-only types.
The resultset.concur_updatable is set to a parameter of the modifiable type.
So if you just want the Result of a type that can be scrolled, just assign the 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 results in a scrollable ResultSet.



3, the ResultSet can be updated
Such a ResultSet object can be done to modify the table in the database, but I know that resultset is only equivalent to the table in the database view, so not all the resultset as long as the settings can be updated to complete the update, The SQL statement that is able to complete the updated ResultSet must have the following properties:
A, only a single table is referenced.
b, does not contain a join or GROUP BY clause.
C, those columns to contain the primary key.
With the above conditions, updatable resultset can complete the modification of the data, and the creation of an updatable result set is:

Statement st = createstatement (result.type_scroll_insensitive,result.concur_updatable)



4, can maintain the resultset
Under normal circumstances, if a query is executed using statement and another query is executed, the result set of the first query is closed, that is, the result set of all statement queries is one, if the commit of connection is called () Method also closes the result set. Retention is when the result of a resultset is submitted, closed or closed. JDBC2.0 and 1.0 provide all the resultset will be closed after submission. However, 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 has three parameters, and this statement is created in the way that I call the third creation of statement.


This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/zeuskaaba/archive/2009/09/04/4517894.aspx

When using resultset, when the query out of the data set a lot of records, there are 10 million of the time, that RS refers to the object will occupy a lot of memory, if the record too much, the program will not put the system's memory run out

No, the resultset surface looks like a recordset, in fact, this object only records the result set of related information, the specific record is not stored in the object, the specific record content know that you are extracted through the next method, By the relevant Getxxxxx method to extract the contents of the field can be obtained from the database, which does not occupy memory, the specific consumption of memory is because you will extract the data in the recordset into your own collection, the time will occur, If you don't use the collection to record all of the records, you will not have a bad memory consumption situation.

How to use Java database programming ResultSet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.