简体   繁体   中英

What does resultSet.getString() return for null value

I'm querying the DB for some values that can potentially be null in the DB. When the I call getString() on the resultSet what will be the value returned by getString if the DB value is null? Here's the javadoc:

Retrieves the value of the designated column in the current row of this ResultSet       
object as a String in the Java programming language.

Parameters:
columnLabel the label for the column specified with the SQL AS clause. If the SQL AS      
clause was not specified, then the label is the name of the column
Returns:
the column value; if the value is SQL NULL, the value returned is null
Throws:
SQLException - if the columnLabel is not valid; if a database access error occurs or    
this    method is called on a closed result set

It says the returned value is null, but I'm not sure if this means "null" or null. I know I can have my code obviously test for both values to see that it is null, but I would prefer it not test for unnecessary conditions.

It says the returned value is null, but I'm not sure if this means "null" or null

In the standard java libraries if it ever says "can return null", it means the reference, aka null .

but I would prefer it not test for unnecessary conditions.

In java if your dealing with null its normally better off to check anyway even if the chance of finding a null is remotely small.

jdbc using mariadb 5.5 on Windows 10 - null column getString() returns the String value "null", and a call to rs.wasNull() immediately after the rs.getString() returns false.

Note: I double checked the database with a query " ... WHERE my-column-name IS NULL" to confirm the database is actually storing it as a null.

So while it's nice to live in an ivory tower and always trust the javadocs, nothing beats writing a quick test yourself. And though I agree coding multiple tests for null is inefficient, unless you can guarantee the specific database/version your app will be running on, it's probably a good idea.

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