0

I have the following SQL line (within a loop):

ResultSet rs = stmt.executeQuery("SELECT * FROM item WHERE itemName='"+ string.get(1) +"'");

string.get(1) contains a different string each time the loop goes. in the current SQL line I will revive only the lines that are equal to string.get(1), but I'd like to get all lines that string.get(1) is a sub string of itemName I know it should go: %string.get(1)% however I don't know the exact syntax.

Anyone can help?

1
  • Do you mean you want to use likeclause of SQL? Commented Jan 7, 2013 at 18:34

2 Answers 2

1

Use the LIKE clause in SQL.

ResultSet rs = stmt.executeQuery
    ("SELECT * FROM item WHERE itemName LIKE '%"+ string.get(1) +"%'");
Sign up to request clarification or add additional context in comments.

7 Comments

This is correct but it will be open invitation for SQL Injection
Only if string.get(1) gets data directly from the user.
hey Erick Robertson, thx for reply. the sql line you wrote is being complied however it doesn't return all the lines it should...
@jeffranz You haven't given me enough information to know why. Try logging the actual queries being executed and run them manually against the database.
@ErickRobertson Thats true but except OP, we don't know that.
|
1

you mean this?

SELECT * FROM TABLE WHERE COL LIKE '%SOME_TEXT%';

Comments

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.