Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
    Bookmark Topic Watch Topic
  • New Topic

Problem in using "IN" in query

 
pax smith
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Report post to moderator
Assuming they are two different values,
use

if 2 & 3 are Strings


otherwise

 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
    Bookmark Topic Watch Topic
  • New Topic