Solved

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

Posted on 2004-04-05
3
211 Views
Last Modified: 2010-04-01
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
Comment
Question by:g118481
  • 2
3 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10761186
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
 
LVL 1

Author Comment

by:g118481
ID: 10762365
Your answer does not make sense to me.
Please clarify.

Cheers
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 150 total points
ID: 10762722
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now