wood burning stoves 2.0*
The moose likes JSP and the fly likes How do I avoid hardcoding column names with SQL taglib Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How do I avoid hardcoding column names with SQL taglib" Watch "How do I avoid hardcoding column names with SQL taglib" New topic
Author

How do I avoid hardcoding column names with SQL taglib

Robert Kostes
Greenhorn

Joined: Feb 04, 2013
Posts: 7
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 10, 2002
Posts: 60794
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Robert Kostes
Greenhorn

Joined: Feb 04, 2013
Posts: 7
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

Joined: Jan 10, 2002
Posts: 60794
    
  65

No one said not to use JSP -- just avoid the SQL tags (and Java scriptlets) in a JSP.
Robert Kostes
Greenhorn

Joined: Feb 04, 2013
Posts: 7
I didn't literally mean straight Java. I just meant JSP without the tags.

thanks
Robert
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I avoid hardcoding column names with SQL taglib
 
Similar Threads
update record for selection of id
Problem connecting to my database.
jdbc connection in jsp
jstl and View data problem
ROWID next "big" thing.