Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Like Clause with mapSqlParameterSource

843859Jul 14 2010 — edited Nov 20 2014
I am trying to use a MapSqlParameterSource to create a query using a Like clause.

The code is something like this. The function containing it receives nameParam:


 String namecount = "SELECT count(*) FROM People WHERE LOWER(NAME) LIKE :pname ";
        
    String finalName= "'%" +nameParam.toLowerCase().trim() + "%'";
    
    MapSqlParameterSource namedParams= new MapSqlParameterSource();
    
    namedParams.addValue("pname", finalName);
    
    int count= this.namedParamJdbcTemplate.queryForInt(namecount, namedParams);
This does not work correctly, giving me somewhere between 0-10 results when I should be receiving thousands. I essentially want the final query to look like:
 SELECT count(*) FROM People WHERE LOWER(NAME) LIKE '%name%'
but this is evidently not happening. Any help would be appreciated.

Edit:

I have also tried putting the '%'s in the SQL, like
     String finalName= nameParam.toLowerCase().trim();
    
     String namecount = "SELECT count(*) FROM People WHERE LOWER(NAME) LIKE '%:pname%' ";
but this does not work either.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 11 2010
Added on Jul 14 2010
0 comments
521 views