开发者

MySQL SMALLINT column returns 0 in java.lang.Short when the value is null

I've com across this and I haven't been able to find the proper documentation.

Basically I have in a MySQL table a column that is used to hold a year value the type of the column is SMALLINT the column can be null, and whenever I retrieve the value (NULL) using the method resultset.getShort("year"); I get a 开发者_如何学C0 instead of the expected null value.

The year value is a member of a bean that means that I use accessor methods, so when I use a CallableStatement object and I set the placeholders accordingly I get a NullPointerException I know this should be expected but there are cases when I need to explicitly set the value to null such as when the year is not supplied or the user enters letters or a string that is not a "year"

Is this the default behavior as the other wrapper classes for primitives do allow null values.

What is the right type mapping to a SMALLINT or should I change the column to INT?


I don't know this library (or Java, for that matter), but I think I found the code here and took a peek. It seems that getShort always returns a short, and never null, but you can use wasNull to check if the last retreived value was actually null.


Check the JDBC documentation. getShort() et al return 0 when the corresponding database column is null. You need to call wasNull() afterwards to check if it was really null.

This is one of the most broken APIs EVER.


You should check the value of resultSet.wasNull to see if you read a NULL or a 0.


Add -Dorg.apache.el.parser.COERCE_TO_ZERO=false to your apache launch configs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜