The iterator is returned after the database is queried.

Source: Internet
Author: User

Since we encapsulate database access, if we query a series of results returned by the database, for example, we get the username of all users from the database, and then display it on the JSP page.

There is a general question here: Is this JavaBean that returns resultset to JSP or collection?

I used to save some time and directly return the resultset, and then traverse a large number of resultsets on my JSP page. This actually obfuscated the data layer and the display layer. In the ejb cmp, the returned collection is reduced in this way. You do not have to modify the program to the front-end JSP page after modifying the database structure. This is no different from the previous php asp development method.

But the return collection efficiency is not very high, because it means that a memory should be opened in the memory to store all the results.

I saw http://builder.com.com/article.jhtml? Id=u00220020814r4b01.htm after this article, I think it is very enlightening to return to iterator.

Iterator is also a mode, and iterator is widely used in jive. I used to wonder why he has nothing to do with writing an iterator himself. Now I know the reason, which saves memory and improves efficiency.

See the following comparison:

Public list getusers (){
Resultset rs = userdbquery ();
List retval = new arraylist ();
While (Rs. Next ()){
Retval. Add (Rs. getstring (1 ));
}
Return retval;
}
The above is the most common method to return the collection. we add the username in the resultset to the list and then return the result. Obviously, this is very memory-consuming.

Use iterator to return:
Public iterator getusers (){
Final resultset rs = userdbquery ();
Return new iterator (){
Private object next;

Public Boolean hasnext (){
If (next = NULL ){
If (! Rs. Next ()){
Return false;
}
Next = Rs. getstring (1 );
}
Return true;
}

Public object next (){
If (! Hasnext ()){
Throw new nosuchelementexception ();
}
String retval = next;
Next = NULL;
Return retval;
}

Public void remove (){
Throw new unsupportedoperationexception ("No remove allowed ");
}
}
}

Here, an internal class is returned. In fact, you can create an iterator class like jive. In this way, it is not so ugly to write here, the iterator you defined does not have any relationship with the iterator in the collection. You have defined three methods hasnext (); next (); remove (); it looks like the iterator of collection.

From the iterator class made by myself, we can see that this JavaBean only performs a pointer transfer function, and passes the pointer that calls this JavaBean to resultset, which improves efficiency and saves memory, this reduces coupling, which is a typical example of middleware.

Since iterator is returned so well, some people often use a simple method to return iterator:
Public iterator getusers (){
Resultset rs = userdbquery ();
List list = new arraylist ()
While (Rs. Next ()){
List. Add (Rs. getstring (1 ));
}
Return list. iterator ();
}

This is actually no different from the direct return list, or a waste of memory.

This article is controversial:

1. Can I disable the database connection rs?

Http://dev.csdn.net/develop/article/17/17705.shtm

As follows:

Will statement and resultset be closed when the close method is called on connection?

Cascade close seems to make sense, and it is also correct in many places.
Connection con = getconnection (); // getconnection is your Method
Preparedstatement PS = con. preparestatement (SQL );
Resultset rs = ps.exe cutequery ();
......
/// Rs. Close ();
/// Ps. Close ();
Con. Close (); // No!
The problem with this is that connection is an interface and its close implementation may be varied. Under normal circumstances, if you use drivermanager. getconnection () to obtain a connection instance, calling its close method will disable statement and resultset. However, in many cases, you need to use the database connection pool,When the close method is called on the connection obtained in the connection pool, the connection may not be released, but return to the connection pool.It may be used by other code in the future. If statement and resultset are not released, more statement and resultset are not closed on the connection ......
On the contrary, I have seen such a saying that some people close the connection but continue to use the resultset. This is acceptable and leads to a heated discussion, I don't have to say anything about it. (I mean: The RS resources are not released, and the Conn in the connection pool is also used)

Therefore, we must release database resources with caution. The following code snippet demonstrates this process.

Connection con = NULL;
Preparedstatement PS = NULL;
Resultset rs = NULL;

Try {
Con = getconnection (); // getconnection is your Method
PS = con. preparestatement (SQL );
Rs = ps.exe cutequery ();
///...........
}
Catch (sqlexception ex ){
/// Handle errors
}
Finally {
Try {
If (PS! = NULL)
PS. Close ();
}
Catch (sqlexception ex ){
/// Handle errors
}
Try {
If (con! = NULL)
Con. Close ();
}
Catch (sqlexception ex ){
/// Handle errors
}
}

Very troublesome, isn't it? But in order to write robust programs, these processes are necessary.

The main idea is as follows:

If you do not use the connection pool mechanism to close the connection, resultset will be disabled!
If the connection pool is used, the so-called close connection actually returns the connection to the connection pool,
The connection object still exists, so this is not actually closed!

2. Performance problems

If it is not for the pattern, use the resultset to traverse is the fastest, which is much faster than put into the collection. Isn't it also the traversal of the resultset to load the collection. It is wrong to traverse the resultset in JSP to occupy database resources. The background Implementation of JSP is servlet. For the performance of the model, the cost is required.

If you still use the resultset method in the collection, there is no performance benefit. The only benefit is that Rs access is blocked for JSP programmers.
Collection needs to wrap the data of Java. SQL. resultset page. Performance.

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.