SQL query inside java

Asked

Viewed 99 times

-1

I have the following query inside a file jsp in Java.

String sql = "SELECT * FROM products WHERE productName = ? ";

I would like my consultation to ignore Camel case and return all the words that were part of the string. For example, Table return table for Camel case and phone return iphone, cellphone, etc...

How can I do that?

I’ve tried to use %, LIKE and it doesn’t work.

  • "Table return table to case" ?!!

  • I think you want to do SELECT * FROM products WHERE LOWER(productName) LIKE '%' + nome + '%';

1 answer

2


To do this, you will need to effectively use the LIKE with the wildcard %.

If you are using a PreparedStatement, which is what it looks like, you’ll have to put the wildcard the value you want to search, thus:

// vamos supor que existe uma variável 'productName' 
// onde teremos o valor a pesquisar
PreparedStatement pstmt = 
    con.prepareStatement("SELECT * FROM products WHERE productName LIKE ?");
pstmt.setString(1, "%" + productName + "%");

This will make a search for the value phone at start, middle and end at all values in the column productName.

  • 1

    thank you very much! it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.