java PreparedStatement 的 setObject() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11793483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setObject() method of PreparedStatement
提问by Java P
Can I use the setObject()
method of PreparedStatement
for all datatypes (like String
, int
or double
)?
我可以对所有数据类型(如、或)使用 的setObject()
方法吗?PreparedStatement
String
int
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);
}
}
}
采纳答案by user207421
I use setObject()
exclusively with MySQL and I've never had a problem with it. I cannot speak for other databases or other vendors.
我setObject()
专门与 MySQL 一起使用,我从来没有遇到过问题。我不能代表其他数据库或其他供应商。
回答by sproketboy
You could potentially have an issue with Blobs or specialized date fields like MSSQL DateTimeOffeet.
您可能会遇到 Blob 或专门的日期字段(如 MSSQL DateTimeOffeet)的问题。
Also I found an issue "Unable to convert between java.lang.Character and JAVA_OBJECT."
我还发现了一个问题“无法在 java.lang.Character 和 JAVA_OBJECT 之间进行转换”。
If the parameter is a single character type.
如果参数是单字符类型。
if (param instanceof Character) {
param = "" + param;
}