my dog learned polymorphism*
The moose likes JSP and the fly likes JSTL and Results Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL and Results" Watch "JSTL and Results" New topic
Author

JSTL and Results

william gates
Ranch Hand

Joined: Feb 21, 2007
Posts: 112
I've finally got JSTL to actually work. But only a simple calculation. Now i'm going into more complex issues and I have a few questions.

From searching various forums, java ranch, and many tutorials it seems most people, when accessing their DB results through beans list something like

<c:forEach>
<td><cut value="${user.firstName} ${user.lastName}" /><br /></td>
<td><cut value="${user.userId}" /></td>

Whereas firstName, lastName and so on are stored and set in a java bean. It makes it easy to retrieve the variable later on by using getWhateverName.

However in my case I don't have the luxury of setting a firstName or lastName.. I access various tables and databases and have a thousand different queries that are used.

I've gotten results to work using jsp scriptlets and just using out.print in java servlets. I run the query and display the results.

It usually looks like

while(resultSet.next())
{
out.println("<TR>");
for(int i=1; i <= columnCount; i++)
{
out.print("<TD>" + resultSet.getString(i));
}
out.println();
}


But in JSTL how would I access the various rows without knowing what the column names are. Like I said it's hard to add exact column names in a javabean. There are thousands of them in various databases.

Some of the dbs and tables change every month, week, and so on. It would be a waste to set up setColumnOne and getColumnOne in a javabean if that column is deleted next week or if they add another column next month. It happens often. New materials, new tools, new equipment, new usages.

It's much easier reading how many columns there are for a table, setting the column names on the fly, then retrieving the results.

I'm not sure how to do this just using JSTL.

Maybe I'm storing my results wrong or maybe I'm storing my bean wrong. I'm really not sure how to move on at this point, except of course not using JSTL at all.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Firstly. resultset and JSP should never be used in the same sentence! It's considered really bad practice to send something as db specific to the UI, not to mention the fact that you are holding the connection open during that time.

Conventionally, a JavaBean is created for each row. But, if as you posted, that's a non-starter, I'd consider a Map for each row. You can query the result set metadata for the names of the columns and included a list of those in conjunction with the map so you'll know what the key/property names will be.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
william gates
Ranch Hand

Joined: Feb 21, 2007
Posts: 112
I've been using JSTL to display my DB results. At first the JSP page using JSTL displayed
-----
{ABC(key)=123(value), XYZ(key)=345(value)}
{AAA=333, BBB=444} and so on for each row.
-----

Then I tried a couple of different things to get each key and value but now it seems I do read each value and key, but display nothing.

Here is my JSTL

<div id="center">
<table border="1">
<c:forEach var='navItem' items='${listResults}' varStatus="navItemStatus">
<c:set var="beanKey" value="${navItem.key}"/>
<c:set var="beanValue" value="${navItem.value}"/>
<tr>
<td><cut value="${beanKey}" />
</td>
</tr>
<tr>
<td><cut value="${beanValue}" />
</td>
</tr>
</c:forEach>
</table>
</div>

I get the <tr><td></td></tr> for all 100 rows, but it only displays blank results.

When I just use <c:forEach var='navItem' items='${listResults}'>
<cut value="navItem" />
</c:forEach>

I get all the rows but it's in the {ABC=123, XYZ=3456} and so on.

What should i change to set the results to look like

Column A Column B Column C Column D
123 456 789 111
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

If listResults is an array (or List) of Maps, you are missing a nested forEach to iterate through the list.
william gates
Ranch Hand

Joined: Feb 21, 2007
Posts: 112
Thanks. I was missing the nested forEach.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL and Results