0

I am trying to run one SQL query using indexed query as, list.add("'%" + some_string + "%'"); and set in query(sample) like :

query=select * from table where name like ?1;

I am setting parameter as :

 query.setParameter(1, list.get(0));

It doesn't work, instead if I put the value directly in query, like :

select * from table where name like '%"+some_string+"%'

It works. I am using JPA Hibernate and postgres database.

Is there any internal parsing of single quotes while setting parameter as

query.setParameter();

1 Answer 1

2

Drop the '' quotes from the placeholder value being added to the List.

list.add("%" + some_string + "%");

Now query.setParameter(1, list.get(0)); should work as expected because any string value being bound to a ? placeholder does not require to be quoted.

Sign up to request clarification or add additional context in comments.

3 Comments

I did that..was working fine! But just worried because I am using LIKE in query which takes parameter in single quotes!
It has nothing to do with LIKE as such. If you had been using any other operator like = or an IN clause, it would still require unquoted string values as parameters for your prepared statement.
@RaviThapliyal How to set parameter in such a way that the single quotes don't get added? I need to pass a column name as a parameter. Can't put the column name as "+ columnName +" 'cause I fetch the query from a JSON. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.