I have the code below in my JSP page.
The problem I am running into, is that when I use a variable in the Order By statement for the query, it doesn't recognize it.
If I hard code the Order By, it works.
This first part of code is looking for the Order_by in the url, if not found it defaults.
The second part of code is my query.
Can anyone see what I am doing wrong?
I outputed the URL variable Order_by to the screen, and it gets passed OK.
If I need to include all my page code, then let me know.
<%
String ORDER_BY = request.getParameter("ORDER_BY");
if( ORDER_BY == null ) ORDER_BY = "CURDATE DESC";
%>
java.sql.ResultSet columns = statement.executeQuery("SELECT * FROM MYTABLE WHERE REQOPEN='OPEN' AND NODENAME='CST' order by '"+ORDER_BY+"'");
In your code above while creating a query you are putting single quotes around the order by variable... and hence your query will not work if you do that..
Assuming that your parameter values comes out to be DESC..
your code will print out
SELECT * FROM MYTABLE WHERE REQOPEN='OPEN' AND NODENAME='CST' order by 'DESC'
while after my changes it will be
SELECT * FROM MYTABLE WHERE REQOPEN='OPEN' AND NODENAME='CST' order by DESC
which is the right syntax for the query..