2

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 0 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?

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, it seems that that is the case, why would be the reason for that behavior ?
I think the creator of that library thinks it is convenient. getShort is actually a wrapper around getString (which will return null if the field is empty). getSHort returns 0 when the field is null or '', but also if a nullpointer exception occurs. All this checking for various exceptions you don't need to do. The library takes care of this for you. All you need to do is call this extra function in the (probably rare) cases where you don't want a default value of 0.
This decision (and others) is described by the author in the header of the library I linked too.
1

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.

1 Comment

Yeah, it's weird, because the BigDecimal type returns null if the column value is null, but the Integer type seems to behave the same way as a Short.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.