Reputation: 2281
Can I use the setObject()
method of PreparedStatement
for all datatypes (like String
, int
or double
)?
What are the potential problems if I use that?
protected void fillStatement(PreparedStatement stmt, Object[] params)
throws SQLException {
if (params == null) {
return;
}
for (int i = 0; i < params.length; i++) {
if (params[i] != null) {
stmt.setObject(i + 1, params[i]);
} else {
stmt.setNull(i + 1, Types.OTHER);
}
}
}
Upvotes: 22
Views: 14488
Reputation: 9452
You could potentially have an issue with Blobs or specialized date fields like MSSQL DateTimeOffeet.
Also I found an issue "Unable to convert between java.lang.Character and JAVA_OBJECT."
If the parameter is a single character type.
if (param instanceof Character) {
param = "" + param;
}
Upvotes: 0
Reputation: 310860
I use setObject()
exclusively with MySQL and I've never had a problem with it. I cannot speak for other databases or other vendors.
Upvotes: 10