This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Set parameter not working in the PreparedStatement , why?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure why my SQL is not working properly after I set parameters in the SQL instead of put variable .

The following is my new code.

String HOLDINGS_QUERY = "select holding_name, holding_value, as_of_date " + "from fund_grp_holding "
+ "where holding_type = ? " + " and fund_grp = ? order by ? " ;

stmt = conn.prepareStatement(HOLDINGS_QUERY);
stmt.setInt(1, type); // type is 6
stmt.setInt(2, fundGroupNumber); // fundGroupNumber is 31
stmt.setString(3, orderyBy); // orderBy is "HOLDING_VALUE DESC"


The search result just ignore "order by" and have a result without ordering my the holding value



The following code will fix the problem and give the search result order by holding value by if I just concat the variable into the query

String HOLDINGS_QUERY = "select holding_name, holding_value, as_of_date " + "from fund_grp_holding "
+ "where holding_type = ? " + " and fund_grp = ? order by " + orderyBy;

stmt = conn.prepareStatement(HOLDINGS_QUERY);
stmt.setInt(1, type); // type is 6
stmt.setInt(2, fundGroupNumber); // fundGroupNumber is 31




WHY? What happened? Anybody can tell me?





 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Order by is a clause and not a parameter that can be set.

Jhakda
 
mooooooo ..... tiny ad ....
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic