There's indeed no straightforward way to do this in JDBC. Some JDBC drivers seem to support PreparedStatement#setArray()
on the IN
clause. I am only not sure which ones that are. I myself just use String#format()
and a helper method to generate the placeholders for IN
clause in a loop (which could be shortened if there was some String#join()
method in Java API) and another helper method to set all the values in a loop.
public static String preparePlaceHolders(int length) {
StringBuilder builder = new StringBuilder(length * 2 - 1);
for (int i = 0; i < length; i++) {
if (i > 0) builder.append(',');
builder.append('?');
}
return builder.toString();
}
public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException {
for (int i = 0; i < values.length; i++) {
preparedStatement.setObject(i + 1, values[i]);
}
}
Here's how you could use it:
import static com.example.SQLUtil.*;
// ...
private static final String SQL_FIND = "SELECT id, name, value FROM entity WHERE id IN (%s)";
public List<Entity> find(Set<Long> ids) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<Entity> entities = new ArrayList<Entity>();
String sql = String.format(SQL_FIND, preparePlaceHolders(ids.size()));
try{
connection = database.getConnection();
statement = connection.prepareStatement(sql);
setValues(statement, ids.toArray());
resultSet = statement.executeQuery();
while (resultSet.next()) {
Enitity entity = new Entity();
entity.setId(resultSet.getLong("id"));
entity.setName(resultSet.getString("name"));
entity.setValue(resultSet.getInt("value"));
entities.add(entity);
}
} finally {
close(connection, statement, resultSet);
}
return entities;
}
Note that some databases have a limit of allowable amount of values in the IN
clause. Oracle for example has this limit on 1000 items.