File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Resultset from Action class to JSP ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Resultset from Action class to JSP ?" Watch "Resultset from Action class to JSP ?" New topic
Author

Resultset from Action class to JSP ?

Kishore Sahu
Ranch Hand

Joined: Mar 10, 2009
Posts: 31
Hi all,
I am using Struts 1.3.10 with mysql DB.
I want to know, how do I pass the resultset from my Action class to my JSP file ?
and Any other method to get the same scenario ?
Please help me as soon as possible.

Thanks in advance.


Kishore
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Passing a ResultSet to a JSP is not a very good idea. I think ResultSet leads to open JDBC connection. I think you must iterate over the result set, Copy the data into a List or any other collection, and then put it into the action context for use the the JSP...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Kishore Sahu
Ranch Hand

Joined: Mar 10, 2009
Posts: 31
Yaah,
How do I push the Resultset (2D) in to an array, can you please show me some code, Please ?
We can not directly copy the resultset to an Array. So how do I push all rows and columns in to an Array and the array should be an 2D type.
Please give me some example.
HL Shen
Greenhorn

Joined: Nov 29, 2007
Posts: 5
Return a list of the JavaBean object and the column is the bean properties.
Then your JSP can use forEach to loop the list to get each JavaBean object in the list and its proeprties.

For ex:


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Create a class to hold the values you need from the ResultSet.
Create a list to hold objects of the new class.
Iterate over the ResultSet, creating a new instance of the new class, copying columns from the ResultSet into the new instance, and put the new instance into the list.

Or use Apache Commons BeanUtils and use a ResultSetDynaClass.
Kishore Sahu
Ranch Hand

Joined: Mar 10, 2009
Posts: 31
What is that Person class ? How it looks like?
I am totally new to all this, please don't get irk and let me ask.
Please help me.
And How do I catch the returned array in JSP and how do I iterate from that result in my JSP ?
Please show me some samples.

Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you also new to Java? If so, it might be a good idea to spend some time covering some important fundamentals before trying to implement anything anybody's going to actually use.

A previous post provided this snippet:This snippet copies values from the result set into an instance of the Person class. From this snippet it's clear the Person class has at least two methods: setFirstName() and setLastName(). In order to retrieve those values on the JSP it must also have getFirstName() and getLastName() methods. This is standard JavaBean conventions; saerching the web for "java bean specification" or simply "java beans" will likely turn up a wealth of resources.

For getFirstName() and getLastName() to work the class must actually store the values, so a reasonable Person class would include at least the following:Does this make sense? If not, there's a Java beginner forum (as well as intermediate and advanced) here on JavaRanch where more general Java questions belong. It's *critically* important to not bite off more than you can chew when it comes to programming: it doesn't make *any* sense to try to write a Java web application if you're not sure how to do even the most basic Java tasks.

Iterating over the list of Person instances in the JSP can be handled with either JSTL's <c:forEach> tag or Struts' <logic:iterate> tag. In general I recommend using JSTL tags when there's an overlap between Struts tags and JSTL tags. Searching the web for "JSTL" or "JSTL forEach" will return many relevant links; this forum (the Struts forum) isn't an appropriate place for general JSTL questions.

Getting the list of Person instances from the Struts action to the JSP can be handled in at least a few different ways: the list can either be part of an ActionForm or set as a request attribute. Personally, I only use ActionForms for form data; other people use it for both form data and for any data that appears in a JSP page whether or not it's part of a form. Which you choose is a matter of preference or the convention followed by wherever you work.

As I mentioned previously you may also use a RowSetDynaClass from the Apache Commons BeanUtils library (actually, I said ResultSetDynaClass, but that's not a good choice for passing to a JSP!) if you don't wish or need to create a separate Person class--it depends on your needs: RowSetDynaClasses aren't always the best solution.

Again--I'd *strongly* urge you to get more familiar with some basic principles of Java, Java web applications, and Struts, before venturing much further down this road. Cobbling functionality together from a group of code snippets isn't the most robust way to learn how things actually work.
Kishore Sahu
Ranch Hand

Joined: Mar 10, 2009
Posts: 31
So, For each JSP page which contains data to be displayed from database, I need to write individual List classes to return the array.
If the table contains 15 columns and I need to display all from Action class to JSP page, then in that case, Shall I need to write class having 15 fields and then return through that ???
Is it a good idea?
Or Any other method to return the array from a resultset ?
Please help me ......
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Kishore if you don't want to use that solution, then you can do something like this (I'm here assuming that all the values from the database will be Strings)



That way you don't need to provide 15 classes for each action but you have to decide which method is better for the long run...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I wrote: As I mentioned previously you may also use a RowSetDynaClass from the Apache Commons BeanUtils library (actually, I said ResultSetDynaClass, but that's not a good choice for passing to a JSP!) if you don't wish or need to create a separate Person class--it depends on your needs: RowSetDynaClasses aren't always the best solution.


Or as the previous commenter mentioned just use a collection/array (seems like a bad idea to me), or use a map so at least the values can be indexed by a reasonable name rather than relying on position.
Kishore Sahu
Ranch Hand

Joined: Mar 10, 2009
Posts: 31
See Below Codes and Please suggest me something.
Kishore Sahu
Ranch Hand

Joined: Mar 10, 2009
Posts: 31
Hi David,

I followed that concept, of creating another class having some setter and getter methods and set resultset values into it.
Now I am getting the Rows and each element.
But I have got one more problem. See below...

Inside Action Class
==============


Output.jsp
========


I got the output
=============


WHY I AM GETTING "KISHORE", 2 times here?
And also sometimes for some other username, I am getting multiple of same records.. like



HERE, Kishore, 3 times and Ravi 2 times.. why ?
Kishore Sahu
Ranch Hand

Joined: Mar 10, 2009
Posts: 31
Hi
I think the above problem, I ll get over it soon.
I have one more problem.

I am getting the records in List and displaying in JSP file using <logic:iterate> and <bean:write>.
The problem is, If I have 5 records in DB table, then those 5 records are being displayed on screen, But If I will do REFRESH the same page, then those 5 records are being added again into the existing list of 5 records.
So Each time, those are getting added to existing, i.e. after 5 times refreshing of the page, I am getting 25 records, having 5 times of 5 records repeatedly.

WHat is the solution to that ?
Please reply me soon.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Resultset from Action class to JSP ?