Tag Archives: Spring JDBC

how to pass LIKE parameters (values) in NamedParameterJdbcTemplate in Spring

Imagine we have a query

SELECT * FROM ARTICLE WHERE TITLE LIKE ‘%spring%’

As we are using NamedParameterJdbcTemplate, I’d like to pass title as a parameter named title holding the value.

So the query would now be SELECT * FROM ARTICLE WHERE TITLE LIKE ‘%:title%’

Solution:
We have to frame the query as like below in Spring JDBC Code

String final GET_QUERY = "SELECT * FROM ARTICLE WHERE TITLE LIKE '%'||:title||'%'";

So, in our spring code

MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("title", title);

NamedParameterJdbcTemplate npjt = new NamedParameterJdbcTemplate(
getJdbcTemplate().getDataSource());
list = npjt.query(
GET_QUERY, parameters,
new RowMapper() {....});