Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Retrieving and displaying ResultSet in JSP

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have built a JSP that can query a database using JDBC and retrieve a ResultSet. I can iterate through that set and display the data out on the page.

I do not want all the Java code exposed on the JSP so I have moved it to a class.

Now I am unsure what is the best method to get the "ResultSet" so that I can display it on the page.

Since a ResultSet object requires executing a Statement, I can't see that I can create a ResultSet object in my JSP code.

I have created a method in my class called "getResultSet". It is current set to a "void" return type.

What is the best way to expose the results of the ResultSet object so that I can access the rows and data elements contained in it, from the JSP?

Do I need to create an object to hold the data from each row and then just create multiple objects? If yes, I am still unsure how I would get the items to display in the HTML of the page.

Thoughts? Advice?

 
Marshal
Posts: 27285
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terry Chambers wrote:Do I need to create an object to hold the data from each row and then just create multiple objects? If yes, I am still unsure how I would get the items to display in the HTML of the page.

Thoughts? Advice?



Yes, that's what you need to do. Create an object to hold the data from each row, and build a List of those objects as you read them from the ResultSet. (An ArrayList would be fine for that.)

And don't do any of that in a JSP. Do it all in a servlet. When you have the List filled up, add it as a variable to the request context. Then forward to a JSP. All the JSP should do is to have some JSTL code which gets that variable from the request context and iterates through it. That way it gets each of the objects you extracted from the ResultSet. If you designed the object so that its class has suitable getter methods, you can use JSTL to get the fields from the object and output them, wrapped in suitable HTML.
 
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul is spot on. Resultsets should never go anywhere near a JSP!

To expand a bit on Paul's post, you'd iterate over the List of objects that capture the data (could be a bean, could be as simple as a string array) using the JSTL's <c:forEach> tag. If your are creating a <table> element to display the info, each iteration would create a new <tr> for the table.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic