• Post Reply Bookmark Topic Watch Topic
  • New Topic

Newbie Q: ok to pass ResultSet from bean to JSP?  RSS feed

 
Kristy McClure
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a newbie question.
In most examples I see, where a bean is used to get a ResultSet from a database, the bean then converts it to something else before it gets passed to the JSP.
Being a newbie, I couldn't get any such thing working, and so am getting a ResultSet into my JSP, then writing it out into an HTML table as usual. It seems to work perfectly fine.
So I wonder, should I NOT use ResultSDets like this? What am I losing by doing it this way?
 
SoonAnn Lim
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In my humble opinion, you are save with this type of coding. However, it is not a efficient way to use Java Bean. Java bean emphasize code reusable component idea in the code. If you use it as resultset, when other people want to use your bean, he/she has to dig into your code to find out what is the data type in the result set. If you convert everything into string before returning the result set, the newcome just have to deal with string array and read the documentation of your bean class. It will save a lot of time. I bet you will not remember what type of result set you get after two years you put your code in production? Right?
Just my thought.
 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi lim,
what i understood from ur reply is that we should mostly return Strings or array of Strings from the beans to be inserted into jsp.Is that the best way out?Did i get that right.Bcoz i also tried an example where the bean method is returning a String which contains a table tag to format the output.How can we retrieve info having several resultant Strings from a bean method(connecting to database and getting results) that are to be properly embedded into a jsp page?
Hope i am clear about my question.
appreciate any help
preeti
 
Vikas Lamba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use either of the 2 methods for your logic. As far as only displaying data is concerned, there should not be any problems with the second approach.
The reason why only the second approach is illustrated in most textbooks is Extensibility. For example, in your case, a requirement might arise to make the HTML table Editable. It would be easier in that case to deal with a bean in the server-side code which would contain methods for updating its entire contents back to the DB.
Sometimes the reason for bringing a JavaBean between ur DB(ResultSet) and the presentation tier(JSP and Servlets) is also to hide the Database structure and its modifications from the front-end. As long as the data-holding Java Beans still expose the same methods as before, the front-end remains unaffected from any Back-end changes(column name change for example).


------------------
Vikas Lamba
 
SoonAnn Lim
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preeti,
You are right, that is what i am trying to say. However, it is not a rule to code this way. Different people have different style. You can return a resultset object from your bean if you are comfortable with. I just don't like to change bean code and jsp code every time i modify my database design or the bean design. You will find it is very difficult to keep track of all your code after your code expands more than 500 lines(blank line not counted *_^). My way of writing code is to do my own style of api documentation, don't have to be fancy, enought for me to understand what i do.
I don't quite understand the second question. I don't see any different if you want to convert table tag into a string object. if you cannot use "+" operator, try using stringbuffer class. ( Hope i make a good guess on your question).
 
Kristy McClure
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soon-Ann: Point taken. I am just lucky thus far that what's in the database (strings and integers) just manage to turn into strings for me when I grab them from the resultset in the JSP. But I can see that many other kinds of values would need special handling pre-display, which would mean putting in a bunch of special code in the JSP where I definitely do not want it.
Thanks for the answer.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!