-1

在我的表定义中,有一列具有 int 数据类型。如果值为“0”,我希望 jdbcTemplate 更新方法默认使用“NULL”而不是“0”更新此字段。

this.jdbcTemplate.update("UPDATE GCUR_OBSERVATION "
                + "SET ObserverId = ?," + "ObservationDate = ?,"
                + "PointCuring = ?"
                + " WHERE locationId = ? AND ObservationStatus = 0",
                new Object[] { new Integer(newObservation.getObserverId()),
                        newObservation.getObservationDate(),
                        new Integer(newObservation.getLocationId()) });

上面的代码片段在Point Curingis时运行良好not NULL。但是,它如何存储NULL在数据库表中呢?

4

2 回答 2

3

我希望你得到 NULL 指针异常。

尝试这个

Integer locId = null;

if(newObservation.getLocationId() != null) {
 locId = new Integer(newObservation.getLocationId());
 if(locId == 0) {
   locId = null;
 }
}

现在在这里传递 locId。

this.jdbcTemplate.update("UPDATE GCUR_OBSERVATION "
                + "SET ObserverId = ?," + "ObservationDate = ?,"
                + "PointCuring = ?"
                + " WHERE locationId = ? AND ObservationStatus = 0",
                new Object[] { new Integer(newObservation.getObserverId()),
                        newObservation.getObservationDate(),
                        locId) });
于 2012-06-29T05:05:24.127 回答
1

假设 getLocationId() 的返回类型是 int 以下代码将起作用。正确的解决方法是将 getLocationId() 的签名更改为 Integer 并确保初始化它的代码将其设置为 NULL 而不是 0

this.jdbcTemplate.update("UPDATE GCUR_OBSERVATION "
                + "SET ObserverId = ?," + "ObservationDate = ?,"
                + "PointCuring = ?"
                + " WHERE locationId = ? AND ObservationStatus = 0",
                new Object[] { new Integer(newObservation.getObserverId()),
                        newObservation.getObservationDate(),
                        newObservation.getLocationId() == 0 ? null : new Integer(newObservation.getLocationId()) });
于 2012-06-29T05:30:04.863 回答