Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sql:query in jsp

 
Bacchi Gerem
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34372
345
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" ?
 
Bacchi Gerem
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that tooo.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic