Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepared Statement with wild card

 
Shriyan San
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a sql query
stmt = conn.prepareStatement("SELECT * FROM TABLE WHERE name LIKE ?");

in my prepared statement i have
stmt.setString(1,"someval");

how do i use LIKE ?% wild card with prepared statement. some reason it's not accepting the % in statement. Help me.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ignore this post - I misread the question]

Shriyan,
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Shriyan San
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you got there Isn't it nice when you get the last step on your own rather than being handed the answer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic