This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I avoid hardcoding column names with SQL taglib

 
Robert Kostes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clapham
Sheriff
Pie
Posts: 20715
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I didn't literally mean straight Java. I just meant JSP without the tags.

thanks
Robert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic