Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using select query with like operator and parameters in java

Tags:

java

sql

mysql

I want to write this query:

SELECT * FROM products
WHERE name LIKE '%?%' 
OR description LIKE '%?%'

and execute it through java in spring web application. But here problem is when I use LIKE operator it has to be with literals, but when I write '%?%' java doesn't recognize ? like placeholders for parameters, when I add parameters I get error that there is no parameters in my query.

Also if I write %?% without literals I get syntax error.

like image 737
wdc Avatar asked Jan 26 '26 10:01

wdc


1 Answers

You can always use concat():

SELECT *
FROM products
WHERE name LIKE CONCAT('%', ?, '%') OR
      description LIKE CONCAT('%', ?, '%');

Or use the application to put the wildcards in the pattern string.

like image 123
Gordon Linoff Avatar answered Jan 29 '26 00:01

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!