I have to format a string such that it can be sent to the IN clause of SQL. String s = ('A','B').This string s shud be passed from java to sql.How can this be done
Asked
Active
Viewed 3,526 times
1
-
1You should know how to do it as you ask a lot of similar questions in the past. – FrVaBe Jan 18 '11 at 08:32
-
Possible duplicate http://stackoverflow.com/questions/178479/alternatives-for-java-sql-preparedstatement-in-clause-issue – Tadeusz Kopec for Ukraine Jan 18 '11 at 09:00
2 Answers
2
I have come across this many times and to the best of my knowledge each element in the set needs to be a separate parameter:
String sql = "select * from customer where city in (?, ?, ?)";
PrepareStatement p = ..;
p.setString("Mumbai");
p.setString("Pune");
p.setString("Bangalore");
...

Miserable Variable
- 28,432
- 15
- 72
- 133
-1
Simply create the SQL statement and append the string to it
String sql = "SELECT a FROM table WHERE a IN "+s;
Now you can create a SQL Statement from this string. A better way may be to use prepared statements...

hage
- 5,966
- 3
- 32
- 42
-
1Avoid creating queries with concatenation. It's dangerous. http://en.wikipedia.org/wiki/SQL_injection – Tadeusz Kopec for Ukraine Jan 18 '11 at 08:56
-
PreparedStatements and IN clauses don't play together very well. – Miserable Variable Jan 18 '11 at 13:04