1

I'm trying to use a named parameter but when I have them inside of single quotation marks they don't get replaced for the query and it returns 0 results. How do I use my parameter inside the single quotation marks?

protected static final String SQL_QUERY =
        "    SELECT * \n" +
        "    FROM example \n" +
        "    WHERE UPPER(name) LIKE UPPER('%:query%') \n";

@Autowired
private NamedParameterJdbcTemplate template;

public List<Item> getResultFromQuery(String query) throws Exception {
return (List<Item>) template.query(SQL_QUERY,
                    new MapSqlParameterSource().addValue("query", query), resultSetExtractor);
}
Icy Creature
  • 1,875
  • 2
  • 28
  • 53

2 Answers2

0

May be you are using \n inside single quotations, you need to encapsulate \n and for the expression '%:query%' you can write it as... '+%:query%+' inside single qutations or may be ' "+%:query%+" '

Ali Haider
  • 41
  • 4
0

MapSqlParameterSource().addValue accepts parametername & value as inputs. Here value type is Object So, mention the type of the parameter you are giving. In your case it is string so give something like String.format(query) and change '%:query%' to :query

    protected static final String SQL_QUERY =
        "    SELECT * \n" +
        "    FROM example \n" +
        "    WHERE UPPER(name) LIKE UPPER(:query) \n";

@Autowired
private NamedParameterJdbcTemplate template;

public List<Item> getResultFromQuery(String query) throws Exception {
return (List<Item>) template.query(SQL_QUERY,
                    new MapSqlParameterSource().addValue("query", String.format(query)), resultSetExtractor);
}