[Java 1.4] How to Embed Lists in PreparedStatement IN Clauses
I’ll introduce how to embed lists in PreparedStatement IN clauses in Java (J2SE 1.4).
Various approaches for embedding lists in PreparedStatement IN clauses are described here, and it was very helpful.
public String preparePlaceHolders(int length) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < length;) {
buffer.append("?");
if (++i < length) {
buffer.append(",");
}
}
return buffer.toString();
}
public void setValues(PreparedStatement preparedStatement, ArrayList values) throws SQLException {
for (int i = 0; i < values.size(); i++) {
preparedStatement.setObject(i + 1, values.get(i));
}
}
That’s all from the Gemba where we want to use Java’s PreparedStatement.