This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes JSP and the fly likes sql:query in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "sql:query in jsp" Watch "sql:query in jsp" New topic

sql:query in jsp

Bacchi Gerem
Ranch Hand

Joined: Jan 23, 2004
Posts: 120
I am using following syntax .But i am not getting table columns in order as like it is in sql database.

How can i get columns in order like it is in sql database?

<sql:query var="deejays"> SELECT * FROM mytable</sql:query>

<%-- Get the column names for the header of the table --%>
<c:forEach var="columnName" items="${deejays.columnNames}"> <th><cut value="${columnName}"/></th></c:forEach>

<%-- Get the value of each column while iterating over rows --%>
<c:forEach var="row" items="${deejays.rows}"> <tr> <c:forEach var="column" items="${row}">
<td><cut value="${column.value}"/></td> </c:forEach> </tr></c:forEach>

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33134

"select *" is risky as you are dependent on an order you don't control. Why not write it more explicitly as "select column1, column2, ... columnN" ?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Bacchi Gerem
Ranch Hand

Joined: Jan 23, 2004
Posts: 120
I tried that tooo.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

JSTL SQL tags were never meant for use in production quality applications.

Here is the top of section 10 in the JSTL spec:

Many web applications need to access relational databases as the source of dynamic
data for their presentation layer. While it is generally preferred to have database
operations handled within the business logic of a web application designed with an
MVC architecture, there are situations where page authors require this capability
within their JSP pages (e.g. prototyping/testing, small scale/simple applications,
lack of developer resources).

The JSTL SQL actions provide the basic capabilities to easily interact with relational

If you need more specific functionality or if this is part of a real app that will need to be maintained and updated over time, you'll do yourself a favor by dumping the JSTL SQL tags now and move to a proper MVC architecture that allows you to control, exactly, how your data is structured before it ever reaches the JSP.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Additionally, because JSTL SQL tags don't fit in with the whole concept of scriptless JSP, very few experienced developers bother with them. Therefore, as this thread shows, it is hard to get help with them on forums like this.
I agree. Here's the link:
subject: sql:query in jsp
It's not a secret anymore!