java 不支持从 UNKNOWN 到 UNKNOWN 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17684734/
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-11-01 14:39:47  来源:igfitidea点击:

The conversion from UNKNOWN to UNKNOWN is unsupported

javasqlsql-serverjdbc

提问by sproketboy

I get the following exception running a stored procedure:

运行存储过程时出现以下异常:

com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from UNKNOWN to UNKNOWN is unsupported.

com.microsoft.sqlserver.jdbc.SQLServerException:不支持从 UNKNOWN 到 UNKNOWN 的转换。

The procedure is defined like this:

该过程定义如下:

CREATE PROCEDURE spTest (
@p1 varchar(1024) ,
@p2 varchar(1024) ,
@p3 char(1) ,
@p4 varchar(254),
@p5 varchar(254),
@debug bit )

My parameters in Java are defined like this:

我在 Java 中的参数定义如下:

Object[] params = {"1,2,3", "d", '2', "", "", 1};

Object[] params = {"1,2,3", "d", '2', "", "", 1};

I think it's caused by the character. Any ideas why?

我觉得是性格造成的。任何想法为什么?

回答by sproketboy

I found it. Clockwork-Muse put me on the path. The char type does not convert to an Object when you set the parameters. The following will work:

我找到了。发条缪斯让我走上正轨。设置参数时,char 类型不会转换为 Object。以下将起作用:

     try (PreparedStatement st = con.prepareStatement(query)) {
        int n = 1;
        for (Object o : params) {

            if (o instanceof Character) {
                o = "" + o;
            }

            st.setObject(n, o);
            n++;
        }
        st.executeQuery();
    }