Do you return to RESULTSET or return Collection?

xiaoxiao2021-03-06  39

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 = UserdbQuery (); list retval = new arraylist (); while (rs.next ()) {RetVal.Add (rs.getstring (1));} Return RetVal; } The top is the most common method of returning Collection to return the username in the resultset to return, apparently this takes a lot of memory. Return to: public iterator getUsers () {Final ResultSet = UserdbQuery (); return new itrator () {private object next; public void Hasnext () {if (next == null) {if (! R. == NULL) {ix ) {RETURN FALSE;} next = rs.getstring (1);} return true;} public object next () {if (! Hasnext ()) {throw new nosuchelementException ();} string return; Return Retval;} public void remove () {Throw new unsupportedOperationException ("no remove allowed");}}} The return is an internal class. In fact, you can do an Iterator class like JIVE, so that you are writing here. Not so ugly, your own defined items and collections have no relationships, and they define three methods havenexT (); next (); remove (); this looks like the collection's Iterator is the same. 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.

转载请注明原文地址:https://www.9cbs.com/read-59618.html

New Post(0)