Forums Register Login

How do I avoid hardcoding column names with SQL taglib

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
No one said not to use JSP -- just avoid the SQL tags (and Java scriptlets) in a JSP.
+Pie Number of slices to send: Send
I didn't literally mean straight Java. I just meant JSP without the tags.

thanks
Robert
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1275 times.
Similar Threads
update record for selection of id
jstl and View data problem
jdbc connection in jsp
Problem connecting to my database.
ROWID next "big" thing.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:54:06.