2

I want a string to be given as input to the SQL "IN" clause,where in i want a list of strings separated by commas

prasanna
  • 51
  • 2
  • 9

6 Answers6

3

You can use guava's Joiner:

Joiner.on(",").join(inputsList);
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
2

This does the job:

public String separateByComma(List<String> input) {

   // check for valid input!!

   public StringBuilder sb = new StringBuilder(input.get(0));
   for (int i = 1; i < input.size(); i++)
     sb.append(",").append(input.get(i));

   return sb.toString();
}

BTW - you may need the Strings boxed in double quotes:

public String separateByCommaForSQL(List<String> input) {

   // check for valid input!!

   public StringBuilder sb = new StringBuilder("\""+input.get(0)+ "\"");
   for (int i = 1; i < input.size(); i++)
     sb.append(",\"").append(input.get(i)).append("\"");

   return sb.toString();
}
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • I have a string where i am splitting the string in array of strings.But how the string is stored is not known.If there are no delimiters in that string ,how can i split the string – prasanna Jan 06 '11 at 05:03
  • @prasanna - *Either* you have delimiters (of some type) *or* the size of each column is fixed (like: name field *always* 25 char). Otherwise you *can't* implement an algorithm to split the string semantically correct into pieces. – Andreas Dolk Jan 06 '11 at 07:18
  • Am storing the selected values of a dropdown in a string.But there is no delimiter between values ,so i cannot split the string.Are there any other methods for the string formatting.or any other ways of storing the string into an array – prasanna Jan 06 '11 at 10:12
  • @prasanna - then *add* a delimiter while concatenating the selected values. Otherwise you have to deal with `String#contains()` expressions to test, if one of the *known!* combo box items was selected. – Andreas Dolk Jan 06 '11 at 10:25
  • the return sb.toString(); returns values like ('ACT','INACT')but while calling the function from java it is called as call function(1,2,('ACT','INACT'),3,4).the problem is the commas inside the brackets.I have to escape those commas.how can i do it ?? – prasanna Jan 17 '11 at 11:23
2

You should use a PreparedStatement so your strings are escaped automatically if necessary and you will be protected from sql injection.

First, generate a statement with a marker for each element in the list. For example:

select id, name from users where name in (?, ?, ?)

Then loop over your list and set the values in the statement.

Take a look at this question for details:

What is the best approach using JDBC for parameterizing an IN clause?

Community
  • 1
  • 1
dogbane
  • 266,786
  • 75
  • 396
  • 414
1

You may use org.apache.commons.lang.StringUtils.join() method, or implement join operation yourself.

Kel
  • 7,680
  • 3
  • 29
  • 39
0

As pointed out by the other answers, there are many helpers you can use to do the joining.

I'd like to add a caveat:

Be aware that most DBMS have a upper limit on the number of values that you can concatenate like this. We have run into this before; if you are not certain that you will never have more than a few dozen values or so, you must make sure you do not exceed this limit (which is different for every DBMS, sometimes even configurable).

The limit usually comes either from a limit on the total size of an SQL statement, or an explicit limit on the number of values in a comma-separated list.

If you run into this problem, an alternative is to create a temporary (or permanent) table for the values list, and INSERT your values into this table. Then you can use a WHERE xxx IN (SELECT column FROM helpertable) instead of putting the list into the SQL statement. This avoids the maximum length problem; it is also probably faster if you need to re-use the list. It' more hassle (and probably slower) if you only need the list once though...

sleske
  • 81,358
  • 34
  • 189
  • 227
0

An unusual solution: If the Strings are in ArrayList or LinkedList (sub class of java.util.AbstractCollection), call toString() which returns "[e1, e2, e3...]". Then you could handle the string.

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130