Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JDBC and the fly likes Prepared Statement with wild card Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Prepared Statement with wild card " Watch "Prepared Statement with wild card " New topic

Prepared Statement with wild card

Shriyan San

Joined: Mar 15, 2005
Posts: 9
I have a sql query
stmt = conn.prepareStatement("SELECT * FROM TABLE WHERE name LIKE ?");

in my prepared statement i have

how do i use LIKE ?% wild card with prepared statement. some reason it's not accepting the % in statement. Help me.
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30136

[ignore this post - I misread the question]

You can't use a wildcard that way. A ? can only substitute for a string literal. So you could do "... where name like ( ?, ?, ? )" if you knew there were going to be three strings.

Without this knowledge, you have to build up the SQL at runtime.
[ June 30, 2005: Message edited by: Jeanne Boyarsky ]

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60800

I see no % anywhere in your code

When used, the wildcards must be part of the parameter string, not the SQL statement.
[ June 29, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Shriyan San

Joined: Mar 15, 2005
Posts: 9
I tried this way it worked

stmt = conn.prepareStatement("SELECT * FROM TABLE WHERE name LIKE ?");

String val="someval";
stmt.setString(1,val+"%" );

Thanks a lot.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Glad you got there Isn't it nice when you get the last step on your own rather than being handed the answer
I agree. Here's the link:
subject: Prepared Statement with wild card
Similar Threads
Is there any limitation in the no of definition entries allowed in tiles.xml file
[Hibernate] Select items starting with an specific letter
Environment Variable problem...
What if search criteria is null?
wild cards in MySQL using jdbc