• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Problem with displaying result of the query - JSTL

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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><c:out 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 c:out 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
 
Sheriff
Posts: 67753
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
What is the data type of each entry? What are the properties? Without that info, we cannot proceed.
 
Reema Roy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it seems odd it would print like that. why dont you try using a bean?
[ June 24, 2008: Message edited by: Arvind Birla ]
 
Reema Roy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont want to use bean .. it is jus a short code!
 
Bear Bibeault
Sheriff
Posts: 67753
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
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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><c:out 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
Sheriff
Posts: 67753
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
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 ]
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic