I am trying to obtain a float value from my Database
The following checks if its null else returns 1; (works if null in DB but errors if a float)
month = row.getString(39) != null ? row.getFloat(39): 1;
On the other hand, the following gets float directly. (works if float in DB but errors if null)
float test = row.getFloat(39);
How would I go about obtaining a float from the DB, and in the case of a null, return a default value. I cant get it in either format as it causes errors when in a diff format.
Preferably without changing the SQL statement. As since there are 45 variables, it would get far too big, messy and unmanageable. I havent looked into it, but I assume with SQL I can do some kind of IFNULL(field,default). Or maybe this is the best way… Although Id probably need AS field on the end too.
The following seems to work for any data type. Using getObject and checking if it is null. Even in the case of floats, where
!= nullwould otherwise be invalid comparison.