File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Reply locked New topic

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,

if 2 & 3 are Strings


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
author & internet detective

Joined: May 26, 2003
Posts: 33134

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Problem in using "IN" in query
jQuery in Action, 3rd edition