• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Prepared Statement with wild card

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic