• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set parameter not working in the PreparedStatement , why?

 
Solo Chiang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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?





 
Jhakda Velu
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Order by is a clause and not a parameter that can be set.

Jhakda
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic