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 prepared statement and IN clause 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 "prepared statement and IN clause" Watch "prepared statement and IN clause" New topic

prepared statement and IN clause

martin samm
Ranch Hand

Joined: Nov 09, 2000
Posts: 31
does anyone know how to handle an IN clause where the number of values in the IN clause may vary when using a PreparedStatement, i.e. i want to code something like :
select * from users where username in (?)
where i want to substitute the ? with any number of values. I realise i could build this up as normal Statement but want to use the efficiency of PreparedStatements. Also i realise i could use and exists and a subselect possibly, but this statement need to be portable accross databases including MySQL
martin samm

martin samm<BR>
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

there is no dynamic way to decide how many ? are needed for each individual query. You have to decide at the time of the prepareStatement() the number of ? needed for your query. You can prepare a new statement for each query though, and append the number of ? needed for that query. You would do use it if you wanted all the benefits of PreparedStatement (such as special character handling) but would not have the all performance benefits as if you reuse the PreparedStatement
but you can't decide after you prepare the preparedStatement

I haven't taken a look at the jdk1.4 yet (and specifically jdbc 3.0) but up to the jdk1.3.x it is not possible
I agree. Here's the link:
subject: prepared statement and IN clause
It's not a secret anymore!