Since we put the database access package, then if the query database returns a series of results, such as the username we get from the database, then display it in the JSP page.
There is a general question here. Is this JavaBean return to the RESULTSET to JSP or Collection?
I used to have time to save trouble, directly returning to ResultSet, and then in my JSP page is a lot of RESULTSET traversal. This is actually confused with the data layer and the display layer. In the EJB CMP, returning is Collection, which is reduced by the coupling, and does not need to modify the program to the front desk JSP page after modifying the database structure, which is not two in the previous PHP ASP development mode.
But returning Collection efficiency is not very high, because it means that you want to open all the results in memory.
I saw http://builder.com.com/Article.jhtml?id=u00220020814R4B01.HTM After this article, I feel that ITERATOR is returned.
Itrator is also a model, which uses Iterator in jive, I used to be very strange, why didn't he write Iterator, now know the reason, save memory, and high efficiency.
Look at the 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 of returning Collection, adding the username in the resultset to return, apparently this is consumed.
Return to Iterator:
Public iterator getusers () {
Final ResultSet RS = UserdbQuery ();
Return new item () {
PRIVATE OBJECT NEXT;
Public void haasnext () {
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";
}
}
}
The return is an internal class. In fact, you can do it like Jive, which is not so ugly, this is not so ugly, your own defined item and collection is there no relationship, it defines three Method HASNEXT (); next (); remove (); which looks like the collection's iTerator.
As seen from this Iterator class, this javabean just made a pointer transfer, transferring the pointer to this javabean to ResultSet, which has improved efficiency, saving memory, and reduced councomibility, this is Typical demonstration of middleware. Since it returns to Iterator, someone often uses a simple return to Iterator method:
Public iterator getusers () {
ResultSet RS = UserdbQuery ();
List list = new arraylist ()
While (rs.next ()) {
List.add (rs.getstring (1));
}
Return list.iterator ();
}
This is actually different from direct returning List, or waste memory.