File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Using Like in preparedstatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Using Like in preparedstatement" Watch "Using Like in preparedstatement" New topic
Author

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
Hi,

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
Bartender

Joined: Dec 20, 2006
Posts: 2516
    
  10

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
youtube channel
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Mahesh Lohi
Ranch Hand

Joined: Jun 22, 2009
Posts: 151
Thank you for your replies.. I got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Like in preparedstatement