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

我只是一个虾纸丫 提交于 2019-12-20 02:57:07

问题


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.


回答1:


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

So:

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


来源:https://stackoverflow.com/questions/6049256/use-of-like-clause-in-sql-prepared-statement-spring-simplejdbctemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!