David Zhao
David Zhao

Reputation: 4514

How to handle null value in a table column with data type float

I have a table "hit", with a column data_value with float data type (default value: NULL):

DROP TABLE IF EXISTS `hit`;
CREATE TABLE  `hit` (
  `hit_id` bigint(20) NOT NULL auto_increment,
  `data_value` float default NULL,
  PRIMARY KEY  (`hit_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

How should I handle this in java when I do: getDataValue()? I'd like it to return NULL if the value is null, but it returns 0.0. Thanks,

David

Upvotes: 1

Views: 1560

Answers (1)

nwinkler
nwinkler

Reputation: 54467

Use one of the uppercase Java classes instead of the primitive, e.g.:

  • Float instead of float
  • Double instead of double
  • BigDecimal

Primitives can't take the value null, they always default to 0 instead. The classes will allow you to use null as well.

Upvotes: 1

Related Questions