Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using prepare statements

 
Peter Warde
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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?
 
Sathya Narayanan Natarajan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JDBC...
 
Lu Battist
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic