The world is so big, and I am so small, I have what face proud, I take what to pride! The only way to gain a little consolation is to keep on learning.
I have always insisted on a theorem: the use of language is always the most reliable official document.
ResultSet:
1, define
ResultSet
A data table that represents a database result set, typically generated by executing statements that query the database.
2, get
Statement stmt = con.createstatement (
Resultset.type_scroll_insensitive,
resultset.concur_updatable);
ResultSet rs = stmt.executequery ("Select a, b from TABLE2");
3, Iteration
ResultSet
object has a pointer to its current data row. Initially, the pointer is placed before the first line. next
method moves the pointer to the next line, because the method ResultSet
returns when there is no next line in the object false
, so it can be used in a while
loop to iterate over the result set.
while (Rs.next ())
{
}
4, take value
ResultSet
Interface provides a fetch method ( getBoolean
,, and so on) for retrieving column values from the current row getLong
. You can use the index number of a column or the name of a column to retrieve a value. In general, the use of column indexes is more efficient. The column is numbered starting at 1. For maximum portability, the result set columns in each row should be read in left-to-right order, and each column can be read only once.
Rs.getstring ();
The above section is from the Chinese version of the JDK1.5 document, if due to the version of the problem caused some problems, please forgive me. There is no exhaustive list of all the information about resultset, and you can browse through the documentation yourself. But we basically only use the parts listed above in the program, I guess in most cases, we rarely use resultset for data updates in Web applications? So I did not list the relevant content of the data update, the interested can be viewed by themselves.
Here's a question: I'm querying all the data in a table from a database, estimating 100 rows, but certainly not knowing exactly how many rows we use to store resultset. So what do I do if I want to take out one of the field values in the first column of the last row? What better way to do it than to iterate? I might even use this requirement multiple times inside a method: I want to arbitrarily remove any column of any row. What do you do, iterate over and over again? Have you considered the issue of database connectivity?
As far as my current level of knowledge is concerned, I am powerless in the existing resultset on the above issues, do not know how to do? I searched through my JDK1.5 's Chinese version of the document and failed to find a good answer, and I immediately felt that the document was outdated. Does Java have a larger usage rate than. NET and can't find a DataTable-like dataset? I guess there is, but the documentation I'm using seems to be for the Java SE version, and it probably doesn't involve more in-depth enterprise applications.
Before explaining the result, let's talk about how we can store the data set now. There is no doubt that in the SSH framework, everyone uses the entity list, so it is convenient to iterate, but also easy to locate, and in the presentation of the use of struts tag, it is very force. However, I basically refuse to use, first, the current SSH with the list using the query Language hql is not good, nor fast (personal feeling). Second, the redundant field, I just want to query a table of two fields, no need to query out the entire table of dozens of fields! Thirdly, what is the perfect solution to the problem of table connection? Is it better than join? Is it really worthwhile for some people to query the resultset themselves and then wrap them separately into the entity list without using an ORM framework?
Result:
1, define
Represents a data table for a database result set. Full name Javax.servlet.jsp.jstl.sql.Result
2, get
To get result, we first have to get the result and then use Resultsupport.toresult (RS) to convert it.
Statement stmt = con.createstatement (
Resultset.type_scroll_insensitive,
resultset.concur_updatable);
ResultSet rs = stmt.executequery ("Select a, b from TABLE2");
Result rst = Resultsupport.toresult (RS);
3, Iteration
Unfortunately, I can not live in the Internet, can not download reading source code, the data is also scarce, degree Niang and Google and search sister in this problem also can not give force, finally only a little try. Kung Fu is not a conscientious, finally I found the following usage:
for (SortedMap item:rst.getRows ()) {
}
This iterative approach is not more complex than resultset, and if you want, you can make a slight change so that the iteration can start from any line or from any end.
4, take value
Because each row in result is sortedmap, the rule is followed:
Object value=item.get (Key);( Other methods of SortedMap are of course applicable)
The introduction of two data sets, I believe you have some understanding. In fact, they have the same function and are similar in usage, and in most cases, there is almost nothing wrong with them.
Now I'm trying to use result to solve the problem that just marked red can't solve.
1, get the last row the first column
Object o= Rst.getrowsbyindex () [Rst.getrowcount ()][0];
Isn't that simple enough?
2. Get any column of any row
Object o= Rst.getrowsbyindex () [row index] [column index];
Note: Be sure to be defensive and avoid throwing exceptions that are beyond the index. Let's develop it slowly after other uses. It is also necessary to note that once we have obtained result from resultset, the database connection can be freed and there will be no usage of the connection.
Some people say I use the entity list, let resultset and result both go to hell.
I don't care! Fast, straightforward, simple, discard redundant fields, I did it right?
As for how to choose, do as you please. The code farmer is also sentimental.
Result and ResultSet in Java