Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaBean Connect to Database, Which should return?

 
xie yufei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Commonly, we use JavaBean to connect to database in Jsp, What should return to the Jsp? Some one say the "ResultSet", and others say use ArrayList, I am confused. The examples:
1、return ResultSet

2、return ArraList
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that we usually return an ArrayList, BUT:
I don't think that it is a good idea to launch a query for each call.
Usually, we only have

where users is a member variable. You should make a DB call only once, before displaying the JSP.
 
xie yufei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where users is a member variable. You should make a DB call only once, before displaying the JSP.


Satou kurinosuke, thanks a lot for your kindness!
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best practice is to write your own Data Access Object (DAO class/ look for DAO pattern). Your DAO class is responsible for accessing the data from the database and map the ResultSet to JavaBean(s) and return a Collection (i.e List, Set, Map etc) of JavaBeans or a single JavaBean.

Note: If you have heard of a O-R(Object to Relational mapping) framework called Hibernate that is what it does. Still you need to use a DAO.
 
xie yufei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arulkumaran Kumaraswamipillai, thank you very much!

Though what you say is difficult for me, I still can learn more fresh knowlege from you words.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic