【发布时间】:2014-04-02 13:28:49
【问题描述】:
我有大量原始数据(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 异常。
知道如何在不改变类型(为字符串)的情况下处理这个问题吗?
最后尝试的选项是,
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
感谢大家的帮助
【问题讨论】: