How to pass NULL value in SqlData implementation for Long typed attribute?

I have a lot of raw data (objects of type SqlData) entering and trying to insert into Oracle using a user-defined type.

The problem is only for records that have some long attributes that are null. I get a NullPointer exception for these entries - since it is trying to call toString for a null object.

Is it possible to write null values ​​for a long column (in a SQLOutput stream)?

I know that this is possible by changing the type of String, but I cannot touch the PL / SQL function, as this is used in many places.

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());

The last line throws a NullPointer exception if getResponseCode () is null.

Any idea how to handle this without changing the type (to String)?

,

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

+4
3

, SQLOutput.writeLong primitive long, long. , , null primitive.

, null check writeLong , , .

+1

NPE - . , - :

if (getResponseCode() != null) stream.writeBigDecimal(new BigDecimal(getResponseCode()));
else stream.writeBigDecimal(null);
+1

You can choose one of the following options:

  • Explicitly using NULL for a null value (use the if statement to select NULL or toString)
  • Eliminate column from insert if null

Something like the following helper class might be useful for you.

public class DatabaseHelper {
private static final String CNULL = "NULL";

static public String fixValue(Object value) {
    if (value == null) {
        return CNULL;
    } else {
        return value.toString();
    }
}
}
0
source

Source: https://habr.com/ru/post/1534655/


All Articles