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?