Home > Software engineering >  How to get both null and 0 for decimal in java
How to get both null and 0 for decimal in java

Time:02-25

I am trying to get retrieve both null and 0.00 for sugar_per_serving into api from the database. The datatype is BigDecimal. Now I put blank in the database, but on the api it is showing 0. Is there a way to change 0 to null? Below is my code:

WeightManagement.java
public BigDecimal sugar_per_serving;

public BigDecimal getSugar_per_serving() {
    return sugar_per_serving;
}

public void setSugar_per_serving(BigDecimal sugar_per_serving) {
    this.sugar_per_serving = sugar_per_serving;
}

PortalDBImpl.java
food.sugar_per_serving=rs.getBigDecimal("sugar_per_serving");

CodePudding user response:

Change only for select:

public BigDecimal getSugar_per_serving() {
    return sugar_per_serving == null ? 0 : sugar_per_serving;
}

Change if you need for update/insert:

public void setSugar_per_serving(BigDecimal sugar_per_serving) {
    this.sugar_per_serving = sugar_per_serving == null ? 0 : sugar_per_serving;
}
  • Related