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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:23:29  来源:igfitidea点击:

setObject() method of PreparedStatement

javajdbc

提问by Java P

Can I use the setObject()method of PreparedStatementfor all datatypes (like String, intor double)?

我可以对所有数据类型(如、或)使用 的setObject()方法吗?PreparedStatementStringintdouble

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;
    }