In Kotlin I would just solve it once and be done with the issue forever with this:
fun <K : Any> ResultSet.getNullable(columnLabel: String, type: KClass<K>): K? =
this.getObject(columnLabel, type.java)
So later you can just do this:
rs.getNullable("ID_PARENT", Int::class)
I guess if you want you could also do this too
fun <K> ResultSet.getNullable(columnLabel: String, type: Class<K>): K? =
this.getObject(columnLabel, type)
So you can just do this:
rs.getNullable("ID_PARENT", Int::class.java)
Or better still make both methods available if you happen to be dealing with developers that can't agree on even the simplest of things.
fun <K : Any> ResultSet.getNullable(columnLabel: String, type: KClass<K>): K? =
this.getNullable(columnLabel, type.java)
fun <K> ResultSet.getNullable(columnLabel: String, type: Class<K>): K? =
this.getObject(columnLabel, type)
Edit: if the library is still being fussy you can finally do something like:
rs.getNullable("ID_PARENT", String::class)?.let {FOO.valueOf(it) }
IF(colName = NULL, 0, colName) AS colName
in theSELECT
statement (preferably in a stored proc). Philosophically this comes down to whether the DB should conform to the app, or vice versa. Since SQL handles NULLs easily and many SQL consumers do not (i.e.java.sql.ResultSet
), I opt to handle it at the DB when possible. (This, of course, assumes that conceptually NULL and zero are equivalent for your purposes.)NULL
and Javanull
. Just write... IS NOT NULL
and retrieve this as aboolean
value in JDBC.