my dog learned polymorphism*
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
JavaRanch » Java Forums » Java » JSP
Bookmark "sql:query in jsp" Watch "sql:query in jsp" New topic
Author

sql:query in jsp

Bacchi Gerem
Ranch Hand

Joined: Jan 23, 2004
Posts: 120
Hi,
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>


Thanks
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Bhaskar,
"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" ?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bacchi Gerem
Ranch Hand

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

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
databases.


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
Sheriff

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sql:query in jsp