简体   繁体   中英

Oracle ResultSet.getString(“colname”) returning the string “null” instead of java null

Edit: Subsequent checking of addrline2 (addrline2 == null) reveals that addrline2 is in fact null, printing out the null object resulting in the string "null" going to stdout.

I'm using the Oracle 11g thin client with an Oracle 11g database. When querying a column that contains null, getString("colname") is return a string "null" instead of a Java null value. Is that the expected behavior? Javadoc indicates I should be receiving a Java null.

    ResultSet rs = descStatement.executeQuery();

    rs.next();
    String addrline2 = rs.getString("addressLine2");
    System.out.println("addrLine2->"+addrline2+"<-");

    boolean wasNull = rs.wasNull();
    System.out.println("Wasnull: "+wasNull);

output:

addrLine2->null<-
Wasnull: true

Note that a null object, when converting to a string, prints out as "null". You're not actually checking for null-ness anywhere.

Try adding

if (addrline2 == null) {
    System.out.println("It's null!")
}

rs.getString("addressLine2") is returning null only but when yo do

System.out.println("addrLine2->"+addrline2+"<-");

its printing null as string literal

do it like this

  System.out.println("addrLine2->"+addrline2!=null ? addrline2 : "Its null"+"<-");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM