Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Print ResultSet data on JSP  RSS feed

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

I am going to get started on coding a JSP page soon. I have about 10 records in the DB which I want to print out on the JSP. Note that I want to spilt the logic from the presentation. So I will need a bean which (1)connects to DB (2) gets the data and throws it into a ResultSet (3) iterate through that ResultSet to print data onto JSP.
My problem is with the 3rd criteria, how would I go about to going through the resultset to print out the data onto the jsp ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't.

In your back-end code, copy the data out of the resultset and into more suitable Java collections or beans immediately, and close the resultset. Pass the copied data to the front-end.

DB resources like resultsets should never be passed to the UI layers.
[ December 25, 2008: Message edited by: Bear Bibeault ]
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically, I would get the data, throw it into a bean, place that bean in an array or something and then iterate through that ?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map it to a collection of row objects and use JSTL's c:forEach tag to iterate over it in JSP.

Your whole logic doesn't sound very good though. Lookup the DAO pattern. You may find this tutorial useful to kickoff with it. It has a JSP/Servlet part as well which also contains a small c:forEach example: http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of collection you use depends upon how much semantics need to propagate to the UI. Much of the time, a good old Object[][] works fine.

And yeah, learning about good back-end patterns like DAOs is a good idea.
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys. Thats a top link you gave there Bauke. Can someone tell me the difference between DAO and DTO ?
Thanks
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be clear too if you read the article text in the given link instead of only the code.
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can see, DAO = An object which is the middleman between the db and DTO. DTO = JavaBean. Am I right ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shaf maff:
From what I can see, DAO = An object which is the middleman between the db and DTO. DTO = JavaBean. Am I right ?


exactly shaf . using the DTO in your project is the best practise .and also to iterate over the collection like List , use the enhanced for loop

[for(DTO to : ArrayList){....}] rather than Iterator .
[ December 25, 2008: Message edited by: seetharaman venkatasamy ]
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Top stuff guys. So I have added a method which gets the data from the DB, for each record it creates a bean, then places that bean into an arraylist and returns that.

I have another set of questions which arose whilst studying the code on the link provided earlier.

1. What is 'Map' ? What is it used for ?
2. When I throw a new exception, and I want to output its message on the JSP, how do I go about doing that ?
3. What does the following code do/mean:



Thanks


.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. What is 'Map' ? What is it used for ?

http://java.sun.com/docs/books/tutorial/collections/interfaces/map.html

2. When I throw a new exception, and I want to output its message on the JSP, how do I go about doing that ?

If you're referring to the JSP/Servlet part of my DAO tutorial, just use the inherited setError() method for that. Also see the registerUser() method here: http://balusc.blogspot.com/2008/07/dao-tutorial-use-in-jspservlet.html#BusinessFormBeanForUser

3. What does the following code do/mean:

http://java.sun.com/docs/books/tutorial/collections/implementations/map.html
[ December 26, 2008: Message edited by: Bauke Scholtz ]
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - I have a question, did you write this without doing a UML diagram ? Or any sort diagrams to make it easy to visualise ? Cause blimy, its a killer trying to workout how it all works. My teachers always told me to create UML diagrams and I never really took notice but now looking at the complexity of your work I can really see its use. I love your style of coding btw!
P.S. Cam you sure your UML (if you have one that is).
[ December 26, 2008: Message edited by: shaf maff ]
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UML is indeed useful for starters.

No, I didn't use it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!