Solved

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

Posted on 2004-04-05
3
215 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Businesses who process credit card payments have to adhere to PCI Compliance standards. Here’s why that’s important.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

766 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