[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

JSP page won't accept a variable in the Order By statement.

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+"'");

0
g118481
Asked:
g118481
  • 2
1 Solution
 
KuldeepchaturvediCommented:
remove the quotes that you are putting in the ORDER_BY
java.sql.ResultSet columns = statement.executeQuery("SELECT * FROM MYTABLE WHERE REQOPEN='OPEN' AND NODENAME='CST'  order by "+ORDER_BY);

that will work

0
 
g118481Author Commented:
Your answer does not make sense to me.
Please clarify.

Cheers
0
 
KuldeepchaturvediCommented:
In SQL you always do the order by column name...
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..
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now