Search code examples
sqlspringjdbcprepared-statementsql-like

Use of LIKE clause in sql prepared statement, spring, SimpleJDBCTemplate


I have the following sql prepared statement:

SELECT * FROM video WHERE video_name LIKE ?

Im using spring and jdbc. i have a method, where term is a searchterm, sjt is a SimpleJdbcTemplate, VideoMapper is a RowMapper and searchForTermQuery is the string from above

...
return sjt.query(searchForTermQuery, new VideoMapper(), term);

My table has 2 videos that match the term. However when I run the query none is found. I get an empty List.

I tried playing with % around the question mark, but it only gave badGrammarExceptions.


Solution

  • You need to put the % around the value itself, not around the placeholder (the question mark).

    So:

    return sjt.query(searchForTermQuery, new VideoMapper(), "%" + term + "%");