0

I want to select all records form database using WHERE IN, using values form an array that I have. Is there a way to actually pass the array to the query? I tried SELECT id FROM tag WHERE name IN "+myArray.toString()+". But, of course, it is destined to fail) I could use preparedStatement, but number of values is always different and quite large - 5000 or so. Or maybe I should take a different approach?

2
  • 2
    You should always use prepared statements instead of string concatenation for SQL creation. Give MyBatis or Hibernate a look if you want a little more flexibility on what you can use as an input. Commented Jul 31, 2015 at 14:33
  • But, I heard that preparedStatement is much slower then string concat. Is it true? (Though, I know preparedStatement is good against sql injections) Commented Jul 31, 2015 at 21:33

1 Answer 1

0

If you don't want to use normal concatenation to do this just use a StringBuilder, which is MUCH more efficient since it only actually creates the string when you use the toString() method.

private String ArrayToString(String[] array)
{
    StringBuilder buffer = new StringBuilder();
    buffer.append(array[0]);

    for (int i = 1; i < array.length; i++)
    {
        buffer.append(",");
        buffer.append(array[i]);
    }

    return buffer.toString();
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.