It's not a secret anymore!*
The moose likes JDBC and the fly likes Problem in using Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Reply locked New topic
Author

Problem in using "IN" in query

pax smith
Ranch Hand

Joined: Dec 13, 2005
Posts: 45
I have created a query and have used 'IN' clause in it. But when I passed pamater "2,3" it is giving me error. Can anybody give any example in which he/she have used 'IN' clause.
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Assuming they are two different values,
use

if 2 & 3 are Strings


otherwise

Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
I think that his problem is related to the PreparedStatement. That he expected that he can use ".. WHERE foo IN (?)" and use a commaseparated string in PreparedStatement#setString().

This is wrong.

You should create as many valueholders as values which you need in the IN clause. Suggestion: collect the values in a Collection (ArrayList? Set?) and pre-create as many valueholders as the Collection´s size and append it to the query string.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Pax,
Please don't post the same question in multiple threads. I'm closing this one and leaving the one in EJB since EJB has more wrinkles than pure JDBC.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in using "IN" in query
 
Similar Threads
Invalid number error
Useing IN clause in named/native query
Reg : Criteria having where clause?
Oracle performance hit for 1 million record
Database search using hibernate