• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I avoid hardcoding column names with SQL taglib

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a JSP file along the lines of:

<%!
public static final String TABLE_SYSDEFAULTS = "app.systemDefaults";

public static final String COL_X = "x";
%>

I then include this in my JSP pages. My update of the table is very clean:

<sql:update var="rowsUpdated">
UPDATE <%=TABLE_SYSDEFAULTS%>
SET <%=COL_X%>='<%=request.getParameter("whatever")%>'
WHERE ID=1
</sql:update>

However, I cannot figure out how to use the constant on the query.

<sql:query var="qrySD" >SELECT * FROM <%=TABLE_SYSDEFAULTS%></sql:query>
<c:forEach var="row" items="${qrySD.rows}">
<c:set var="rowId" value="${row.x}"/>
...

How can I use row.COL_X rather than hardcoding the column name?

Robert
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, for a start I wouldn't mix scriptlets with JSTL. In fact I wouldn't use scriptlets at all. I would do this to declare the column name:



and then I would do this to access it:



Or something like that -- I'm not really familiar with the SQL tags. A proper solution to this question would involve not using JSP at all, since none of what you are doing there has anything to do with generating HTML. It should really be done in a servlet, or in some other component called from a servlet.
 
Sheriff
Posts: 67747
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
Be aware that even the framers of the JSTL Specification recommend against using the SQL tags in a JSP for anything other than quick prototype code.
 
Robert Kostes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick responses. I am generating HTML but I did not want to bore you with the details.

I got rid of the SQL tags and am using straight Java and HTML. It is much longer code but it all works nicely.
The combination of Java and HTLM is not as ugly as I thought it would be.

thanks
Robert
 
Bear Bibeault
Sheriff
Posts: 67747
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
No one said not to use JSP -- just avoid the SQL tags (and Java scriptlets) in a JSP.
 
Robert Kostes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't literally mean straight Java. I just meant JSP without the tags.

thanks
Robert
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic