Create the String separately:
        //must have at least one.
        StringBuffer sb = new StringBuffer("SELECT * FROM articles ORDER BY
?");

        //go through list of sorts
        int lvls = sortlvls.size() - 1;

        for( int i = 0; i < lvls; i++ )
        {
                sb.append(",?");
        }

        //make preparedstatement from stringbuffer
        preparedStatement = conn.prepareStatement(sb.toString());

-Tim

-----Original Message-----
From: Mattias Jiderhamn [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, November 28, 2001 10: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