-----Original Message-----
From: David Nguyen [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, November 28, 2001 11:22 AM
To: [EMAIL PROTECTED]
Subject: RE: [OffTopic] ORDER BY in PreparedStatement


Mattias,

I think you can simply use Statement instead of PreparedStatement. The point
here is how to compose the query dynamically.
I would do something like this:

String criteria[]; // supposed to contain order-by criteria fed from
servlet/jsp
...
String q = "SELECT * FROM articles";
boolean isFirst = true;

while (i=0; criteria.length > 0; i++) {
  if (isFirst) {
    q += " ORDER BY " + criteria[i];
    isFirst = false;
  } else
    q += ", " + criteria[i];
}

If you still want to use PreparedStatement you can do similarly: just ad
some yourPreparedStatement.setString(i, criteria[i]);

david

-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Mattias Jiderhamn
Sent: Wednesday, November 28, 2001 7:38 AM
To: [EMAIL PROTECTED]
Subject: [OffTopic] ORDER BY in PreparedStatement


I know this really is off topic, but with the discussion about user
authentication and PreparedStatements some two weeks ago in mind, I
thought maybe somebody here can answer a quiestion.

How do I use ORDER BY with PreparedStatement?

We use this in a web app where the user should be able to change sorting
in a "Windows Explorer"-like fashion.
The problem occurs when I want to use multiple sorting fields.

Example:
If I have
  preparedStatement = conn.prepareStatement("SELECT * FROM articles
ORDER BY ?");
then
  preparedStatement.setString(1, "articleId");
works fine but
  preparedStatement.setString(1, "articleText, articleId");
doesn't (which actually isn't very surprising).

I haven't tried
  conn.prepareStatement("SELECT * FROM articles ORDER BY ?, ?");
  preparedStatement.setString(1, "articleText");
  preparedStatement.setString(2, "articleId");
since it depends on what the user clicks how many fields we want in
there.

How do I solve this? Is it driver dependent?

  Mattias Jiderhamn
  Expert Systems
  [EMAIL PROTECTED]

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to