creating comma seperated string to be given as input to sql “IN” clause

前端 未结 6 1958
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 03:18

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

相关标签:
6条回答
  • 2021-01-23 03:43

    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();
    }
    
    0 讨论(0)
  • 2021-01-23 03:43

    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.

    0 讨论(0)
  • 2021-01-23 03:45

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

    0 讨论(0)
  • 2021-01-23 04:00

    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?

    0 讨论(0)
  • 2021-01-23 04:02

    You can use guava's Joiner:

    Joiner.on(",").join(inputsList);
    
    0 讨论(0)
  • 2021-01-23 04:02

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

    0 讨论(0)
提交回复
热议问题