This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Kubernetes in Action and have Marko Luksa on-line!
See this thread for details.
Win a copy of Kubernetes in Action this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Prepared Statement with wild card  RSS feed

 
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.
 
author & internet detective
Marshal
Posts: 37714
579
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 ]
 
Author and ninkuma
Marshal
Posts: 66352
155
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.
 
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
Boost this thread!