Java PreparedStatement setNull(..)

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

PreparedStatement setNull(..)

javajdbcprepared-statement

提问by paweloque

Java PreparedStatement provides a possibility to explicitely set a Null value. This possibility is:

Java PreparedStatement 提供了显式设置 Null 值的可能性。这种可能性是:

prepStmt.setNull(parameterIndex, Types.VARCHAR);

Are the semantics of this call the same as when using a specific setType with a null parameter?

此调用的语义与使用带有空参数的特定 setType 时的语义相同吗?

prepStmt.setString(null);

?

?

采纳答案by djna

This guidesays:

指南说:

6.1.5 Sending JDBC NULL as an IN parameter

6.1.5 将 JDBC NULL 作为 IN 参数发送

The setNull method allows a programmer to send a JDBC NULL (a generic SQL NULL) value to the database as an IN parameter. Note, however, that one must still specify the JDBC type of the parameter.

setNull 方法允许程序员将 JDBC NULL(通用 SQL NULL)值作为 IN 参数发送到数据库。但是请注意,仍然必须指定参数的 JDBC 类型。

A JDBC NULL will also be sent to the database when a Java null value is passed to a setXXX method (if it takes Java objects as arguments). The method setObject, however, can take a null value only if the JDBC type is specified.

当 Java 空值传递给 setXXX 方法时,JDBC NULL 也将发送到数据库(如果它接受 Java 对象作为参数)。但是,仅当指定了 JDBC 类型时,方法 setObject 才可以采用空值。

So yes they're equivalent.

所以是的,它们是等价的。

回答by paweloque

Finally I did a small test and while I was programming it it came to my mind, that without the setNull(..) method there would be no way to set null values for the Java primitives. For Objects both ways

最后我做了一个小测试,当我在编程时,我想到如果没有 setNull(..) 方法,就无法为 Java 原语设置空值。对于对象双向

setNull(..)

and

set<ClassName>(.., null)) 

behave the same way.

以同样的方式行事。

回答by Owen

but watch out for this....

但要注意这一点......

Long nullLong = null;

preparedStatement.setLong( nullLong );

-thows null pointer exception-

-出现空指针异常-

because the protype is

因为原型是

setLong( long )   

NOT

不是

setLong( Long )

nice one to catch you out eh.

很高兴能抓住你,嗯。

回答by bithom

You could also consider using preparedStatement.setObject(index,value,type);

你也可以考虑使用 preparedStatement.setObject(index,value,type);

回答by Dmitriy Pichugin

preparedStatement.setNull( index, java.sql.Types.NULL );

PreparedStatement.setNull( index, java.sql.Types.NULL );

that should work for any type. Though in some cases failure happens on the server side, like: for SQL:

这应该适用于任何类型。尽管在某些情况下失败发生在服务器端,例如:对于 SQL:

COALESCE( ?, CURRENT_TIMESTAMP )

合并(?,CURRENT_TIMESTAMP)

Oracle 18XE fails with wrong type: expected DATE, got STRING -- that is perfectly valid failure;

Oracle 18XE 失败,类型错误:预期日期,得到字符串——这是完全有效的失败;

Bottom line: it is good to know the type if you call .setNull()

底线:如果您调用 .setNull() 最好知道类型