• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

using prepare statements

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a prepared statement that has a sql query such as

SELECT *
FROM table1
WHERE name IN( ? )

and I have an array String [] names.

How do I pass the array names as a parameter to the prepared statement?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

From the String[] Array, Construct a Comma-Separated String Of Names in the following format ,

'Name1','Name2','Name3'

The following code will help you get a Comma-Separated-String of Names

String param = stringArray[0];
for (int i = 1; i<stringArray.length; i++) {
param += stringArray[i];
}


Then, just set the param in the statement as follows,

preparedStatement.setString(1,param);
Then run the query.

Hope this helps you,
Thanks.
Sathya
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I don't think you can do that, set the list of strings for an "in" condition. You might have to build your SQL by hand then. Just make sure you escape any single quote characters.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to JDBC...
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will work better, but if you expect nulls in the stringArray
you may need to test for that and use the preparedStatement.setNull(...) method.


[ March 16, 2005: Message edited by: Lu Battist ]
 
Grow your own food... or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic