The moose likes JDBC and Relational Databases and the fly likes Using Like in preparedstatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Using Like in preparedstatement" Watch "Using Like in preparedstatement" New topic

Using Like in preparedstatement

Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 151
Hi all,

Can some one plzguide on the usage "Like" clause in preparedstatement

I just used

I got an error
Parameter index out of range (1 > number of parameters, which is 0).

Thank you advance
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423

change your query to this:

? is bind parameter- it is like variable in java, if you use it within quotes, database treats it as literal string.
Similary in java if you put the variable name inside double quotes, compiler doesn't see it, like in this example:
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

According to this Oracle article, you can just use a bind variable for LIKE.

You might have to change your code to (untested pseudocode):

OCUP UML fundamental and ITIL foundation
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

Jan is correct, you just need to use the wildcards in the variable you pass to the preparedstatement. Keep in mind, though, that wild cards are not required with LIKE clauses. For example putting a wildcard only at the end of a query finds all matches that start with the word.

[OCA 8 Book] [Blog]
Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 151
Thank you for your replies.. I got it
I agree. Here's the link:
subject: Using Like in preparedstatement
It's not a secret anymore!