Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

prepared statement and IN clause

martin samm
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic