1

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

2

2 Answers 2

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");
...
Sign up to request clarification or add additional context in comments.

Comments

-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...

2 Comments

Avoid creating queries with concatenation. It's dangerous. en.wikipedia.org/wiki/SQL_injection
PreparedStatements and IN clauses don't play together very well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.