[Java 1.4] How to Embed Lists in PreparedStatement IN Clause
I’ll introduce a method for embedding lists in PreparedStatement IN clauses in Java (J2SE 1.4).
This resource describes various approaches for embedding lists in PreparedStatement IN clauses and 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.
That’s all from the Gemba.