我有大量原始数据(SqlData 对象类型)传入并尝试使用创建的用户定义类型插入到 Oracle 中。
问题仅针对具有一些为空的 Long 属性的记录。 我得到了这些记录的 NullPointer 异常 - 因为它试图在空对象上调用 toString。
有没有办法为 Long 列(在 SQLOutput 流中)写入空值?
我知道这可以通过更改为 String 类型来实现,但我无法触及 PL/SQL 函数,因为它在很多地方都被使用。
public classMyClass extends MyBaseType implements SQLData {
public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeString(getSource());
stream.writeTimestamp(getDtm());
stream.writeString(getUniqueId());
stream.writeString(getType());
stream.writeLong(getResponseCode());
当 getResponseCode() 为 null 时,最后一行导致 NullPointer Exception。
知道如何在不更改类型(到字符串)的情况下处理这个问题吗?
最后尝试的选项是,
private void writeNullPossibleNumber(SQLOutput stream, Integer intValue) throws SQLException {
if (intValue==null) {
stream.writeBigDecimal(null);
} else {
stream.writeInt(intValue);
}
}
上述解决方案工作正常。
引用: http://docs.oracle.com/cd/A87860_01/doc/java.817/a81357/sampcod3.htm
感谢大家的帮助
最佳答案
如您所见,SQLOutput.writeLong
等待 primitive
long
,但您似乎尝试提供包装器 Long
.当然,在这种情况下,null
不能转换为任何 primitive
。
所以,或者只是在 writeLong
之前添加 null check
,或者更改整个逻辑,如果您根本无法将 null 写入 DB。
关于java - 如何在 Long 类型属性的 Sql 数据实现中传递 NULL 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22813030/