Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with displaying result of the query - JSTL

 
Reema Roy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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><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
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Arvind Mahendra
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I dont want to use bean .. it is jus a short code!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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><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
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stefan Evans
Bartender
Posts: 1721
10
  • Mark post as helpful
  • send pies
  • 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).

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic