java 在 PreparedStatement setObject 方法中传递空值给出异常

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

passing null value in PreparedStatement setObject method gives exception

javanullprepared-statement

提问by vkumar

Do we have a solution for this?

我们有解决方案吗?

my code is something like this:

我的代码是这样的:

preparedStatement.SetObject(i , MyArray);

Here , MyArray is an array of record fetched form a table. Now , whenever the above statement finds a value null it throws an SQL exception : invalid Column type.

这里,MyArray 是从表中获取的记录数组。现在,只要上面的语句发现一个 null 值,它就会抛出一个 SQL 异常:无效的列类型。

Options available are to use setObject(int parameterIndex, Object x, int sqlType) or setNull , but in that case i need to provide SQL type of the target column, which doesn't seem possible.

可用的选项是使用 setObject(int parameterIndex, Object x, int sqlType) 或 setNull ,但在这种情况下,我需要提供目标列的 SQL 类型,这似乎不可能。

Thanks

谢谢

回答by Oleg Mikheev

you can try using setNull with NULL type:

您可以尝试将 setNull 与 NULL 类型一起使用:

if(myArray == null) {
    preparedStatement.setNull(i, Types.NULL);
}

And please follow Java coding standards - variables and methods are initial lower case!

并且请遵循 Java 编码标准——变量和方法都是小写的!

回答by Abhinav

Try:

尝试:

preparedStatement.setObject(i , MyArray, java.sql.Types.ARRAY);

With the info available this is all I can come up with. Hopefully, this should work.

有了可用的信息,这就是我所能想到的。希望这应该有效。

Abhinav

阿比纳夫