Solved

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

Posted on 2004-04-05
3
209 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
Comment Utility
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
Comment Utility
Your answer does not make sense to me.
Please clarify.

Cheers
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 150 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

8 Experts available now in Live!

Get 1:1 Help Now