aspose file tools*
The moose likes JSP and the fly likes Problem with displaying result of the query - JSTL    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem with displaying result of the query - JSTL    " Watch "Problem with displaying result of the query - JSTL    " New topic
Author

Problem with displaying result of the query - JSTL

Reema Roy
Greenhorn

Joined: Jun 24, 2008
Posts: 4
Hey Guys
I am trying to get the output from a query and display it in a drop down list box.
I do get the correct output but the problem is I do not get in the format i desire.

This is where I display the output of the query
<select name="dropdown">
<c:forEach var="row" items="${data.rows}">
<option><cut value="${row}"/></option>
</c:forEach>
</select>

This query is "select Name from Employee" and the output will simply be a list of names. The query is dynamic and might change as I am pulling it from xml.
SO in cut I cannot use row.name and if I jus use "${row}" then I get the output as "{name=Albert}" I just want Albert as my output.. How do I achieve that?
I even tried row.value which does not work either.
Any help will be appreciated!
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61626
    
  67

What is the data type of each entry? What are the properties? Without that info, we cannot proceed.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Reema Roy
Greenhorn

Joined: Jun 24, 2008
Posts: 4
Thanks for your reply!

The datatype inside the database is String.
There are no other properties, it pulls in the information that is in String format from the database.
As I already said it pulls in the correct information but I jus want the name of the employee and not the name of the column.
I could do data.name but everytime I have a different query which I pull in from xml so I cannot hardcode .name

Any Suggestions?
Thanks a Lot!
Arvind Mahendra
Ranch Hand

Joined: Jul 14, 2007
Posts: 1162
it seems odd it would print like that. why dont you try using a bean?
[ June 24, 2008: Message edited by: Arvind Birla ]

I want to be like marc
Reema Roy
Greenhorn

Joined: Jun 24, 2008
Posts: 4
I dont want to use bean .. it is jus a short code!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61626
    
  67

The type of "data" cannot be a string, otherwise you;d not be able to iterate over it (except as characters). What is data? An array. Of what? Strings? An array of strings?

You need to completely describe the data that you are sending to the JSP.
Reema Roy
Greenhorn

Joined: Jun 24, 2008
Posts: 4
Hey Thanks for your response !

The data is not an array it is just a result set retrieved from the query.
The value of the variable sql query is a query retrieved from xml.

<sql:query var="data" dataSource="${connection}" sql="${sqlquery}" />

<select name="dropdown">
<c:forEach var="row" items="${data.rows}">
<option><cut value="${row}"/></option>
</c:forEach>
</select>

This gives me a list box in the output which contains:
{Name=Albert}
{Name=John}

I just want names and not the column names in the output.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61626
    
  67

Resultsets are not suitable constructs to be sending to a JSP. Not only do you hold DB resources open when you hang onto a result set for so long, the non-bean structure of the set is not suited for accessing through the EL.

Best practices dictate that, as soon as feasible, you copy the data from result sets into more suitable containers (array, lists, maps, and beans, as Arvind indicated) and pass those to the higher layers.

A fairly good test of appropriate Separation of Concerns is to make sure that nothing in package java.sql (or javax.sql) is exposed outside of the data layer.
[ June 24, 2008: Message edited by: Bear Bibeault ]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Writing sql queries on your jsp page is not the best way to go, but if you're going to do it, at least you're using the jstl tags for it.

Instead of using ${data.rows} (which gives you a Map, keyed by column name) you could use ${data.rowsByIndex} (gives you an array). You can then just always print out the first column (column 0).

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem with displaying result of the query - JSTL