File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I avoid hardcoding column names with SQL taglib

 
Robert Kostes
Greenhorn
Posts: 7
  • 0
  • 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: 20170
24
MySQL Database
  • 0
  • 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: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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
  • 0
  • 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: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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
  • 0
  • 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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic