Re: PreparedStatement + "order by ?"
- From: "Adam Maass" <adam.nospam.maass@xxxxxxxxxxx>
- Date: Fri, 17 Feb 2006 21:50:02 -0800
"Chris" <krystofffff@xxxxxxxxx> wrote:
Hi
Is there a way to use a PreparedStatement with a query such as
"SELECT * FROM table ORDER BY ?;"
where the first parameter is the name of the table field I'd like to
order the results to ?
I tried to following code :
PreparedStatement p = new PreparedStatement("SELECT * FROM table ORDER BY
?");
p.setString(1,"name");
The query seems to properly compile and execute, but the ResultSet is
not ordered as it should be :(
I'm surprised it worked at all. See below.
Moreover, I'd like to do something like :
PreparedStatement p = new PreparedStatement("SELECT * FROM table ORDER BYBut I think it is really impossible this time ...
? ?");
p.setObject(1,"name");
p.setObject(2,"ASC");
Short answer: no, you can't do this.
Longer answer: The '?' syntax in PreparedStatements is intended to be used
for input parameters (only). (IE, what Oracle calls 'bind variables' --
other databases have different names for essentially the same concept.) They
are not a general-purpose placeholder for whatever bit of SQL syntax you
might find handy.
It might be possible to find a JDBC driver/database that supports the '?'
syntax to do what you propose, but it would be a non-standard feature that
wouldn't be portable to most other databases.
-- Adam Maass
.
- Follow-Ups:
- Re: PreparedStatement + "order by ?"
- From: Owen Jacobson
- Re: PreparedStatement + "order by ?"
- References:
- PreparedStatement + "order by ?"
- From: Chris
- PreparedStatement + "order by ?"
- Prev by Date: Re: SSL Client with client certificate
- Next by Date: Re: Java 5 classloader not making static calls.
- Previous by thread: Re: PreparedStatement + "order by ?"
- Next by thread: Re: PreparedStatement + "order by ?"
- Index(es):
Relevant Pages
|