|
|
retrieving null values from ResultSet
Hi,
Firstly, this question is really about ResultSet, but I encountered it while implementing ResultSetExtractor, so I guess it's vaguely relevant to this forum. Anyway.....
When I call ResultSet.getInt() the method will return 0 if the column value is either 0 or null, which is extremely inconvenient, because in many (maybe most) cases, one needs to distinguish between 0 and null. Now I realise that I can use ResultSet.wasNull() to distinguish between 0 and null, but what I don't understand is why the API was defined like this?
If ResultSet.getInt() was defined to return an Integer instead of an int, then it could return either 0 or null, as appropriate, so why wasn't it done like this? This seems like such an obvious mistake that I'm sure I must be missing something. If so, please let me know what it is and feel free to mock my ignorance
Cheers,
DM
This is a very common case; you need to define a null representation in the database. Take a negative value to represent null, and get your app to detect it.
Especially useful it the situations when negative values are perfectly legal
And as for original question - I may guess only, but API was defined long ago, when no autoboxing has existed so usage of Integer would cause inconvinieces on any place where this Integer will be processed and not only on place where it is created/populated). As well creation of Integer was (and to some extent still be) rather expensive (comparing to int).
Originally Posted by kantornThis is a very common case; you need to define a null representation in the database. Take a negative value to represent null, and get your app to detect it.
Is there anything in Spring-JDBC to alleviate this problem? If there were, it would probably be called something like NullAwareResultSetExtractor
If not, could this be a useful addition to a future release?
Originally Posted by domurtagIs there anything in Spring-JDBC to alleviate this problem? If there were, it would probably be called something like NullAwareResultSetExtractor
If not, could this be a useful addition to a future release?
You may take a look on JdbcUtils.getResultSetValue(),
ColumnMapRowMapper class
And do not forget that JDBC itself contains quot;one-and-for-allquot; getObject() method that you may apply to integer columns with result that would satisfy your taste - Integer which is null if column is null. You only need to apply cast toi the result of this method. |
|