代码之家  ›  专栏  ›  技术社区  ›  ian_scho

从Java结果集中检查空int值

  •  245
  • ian_scho  · 技术社区  · 16 年前

    在Java中,我试图从结果集中测试一个空值,其中将列转换为一个基元。 int 类型。

    int iVal;
    ResultSet rs = magicallyAppearingStmt.executeQuery(query);
    if (rs.next()) {
      if (rs.getObject("ID_PARENT") != null && !rs.wasNull()) {
        iVal = rs.getInt("ID_PARENT");
      }
    }
    

    从上面的代码片段来看,有没有更好的方法可以做到这一点,我假设第二个 wasNull() 测试是多余的?

    教育我们,谢谢

    9 回复  |  直到 9 年前
        1
  •  319
  •   Richard    9 年前

    默认值为 ResultSet.getInt 当字段值为 NULL 就是回归 0 ,这也是您的 iVal 宣言。在这种情况下,您的测试是完全冗余的。

    如果字段值为空,则实际需要执行不同的操作,我建议:

    int iVal = 0;
    ResultSet rs = magicallyAppearingStmt.executeQuery(query);
    if (rs.next()) {
        iVal = rs.getInt("ID_PARENT");
        if (rs.wasNull()) {
            // handle NULL field value
        }
    }
    

    (以下编辑为@martin注释;编写的操作代码不会编译,因为 伊瓦尔 未初始化)

        2
  •  75
  •   felipe.zkn Redrif    11 年前

    另一个解决方案:

    public class DaoTools {
        static public Integer getInteger(ResultSet rs, String strColName) throws SQLException {
            int nValue = rs.getInt(strColName);
            return rs.wasNull() ? null : nValue;
        }
    }
    
        3
  •  27
  •   Andreas Dolk    12 年前

    我认为这是多余的。 rs.getObject("ID_PARENT") 应该返回 Integer 对象或 null ,如果列值实际为 NULL . 所以它甚至应该可以做如下的事情:

    if (rs.next()) {
      Integer idParent = (Integer) rs.getObject("ID_PARENT");
      if (idParent != null) {
        iVal = idParent; // works for Java 1.5+
      } else {
        // handle this case
      }      
    }
    
        4
  •  22
  •   BalusC    11 年前

    只需检查字段是否为 null 或不使用 ResultSet#getObject() . 代用品 -1 使用所需的空大小写值。

    int foo = resultSet.getObject("foo") != null ? resultSet.getInt("foo") : -1;
    

    或者,如果可以保证使用正确的db列类型,那么 结果集GetObject() 真的返回一个 Integer (而不是) Long , Short Byte ,然后您也可以将其类型化为 整数 .

    Integer foo = (Integer) resultSet.getObject("foo");
    
        5
  •  8
  •   Peter Tillemans    16 年前

    Afaik你可以简单地使用

    iVal = rs.getInt("ID_PARENT");
    if (rs.wasNull()) {
      // do somthing interesting to handle this situation
    }
    

    即使它是空的。

        6
  •  3
  •   luchoct    9 年前

    只是用Java泛型进行更新。

    您可以创建一个实用方法来从一个给定的结果集中检索任何Java类型的可选值。

    不幸的是,GETObjor(ClulnNeX,Class)不返回null,而是给定Java类型的默认值,因此需要2个调用。

    public <T> T getOptionalValue(final ResultSet rs, final String columnName, final Class<T> clazz) throws SQLException {
        final T value = rs.getObject(columnName, clazz);
        return rs.wasNull() ? null : value;
    }
    

    在本例中,您的代码可能如下所示:

    final Integer columnValue = getOptionalValue(rs, Integer.class);
    if (columnValue == null) {
        //null handling
    } else {
        //use int value of columnValue with autoboxing
    }
    

    很高兴得到反馈

        7
  •  1
  •   Jacob Crofts    9 年前

    为了方便起见,可以在resultset周围创建一个包装类,当 ResultSet 通常不会。

    public final class ResultSetWrapper {
    
        private final ResultSet rs;
    
        public ResultSetWrapper(ResultSet rs) {
            this.rs = rs;
        }
    
        public ResultSet getResultSet() {
            return rs;
        }
    
        public Boolean getBoolean(String label) throws SQLException {
            final boolean b = rs.getBoolean(label);
            if (rs.wasNull()) {
                return null;
            }
            return b;
        }
    
        public Byte getByte(String label) throws SQLException {
            final byte b = rs.getByte(label);
            if (rs.wasNull()) {
                return null;
            }
            return b;
        }
    
        // ...
    
    }
    
        8
  •  0
  •   George    10 年前

    用Java 8可以做到这一点:

    Long nVal = Optional.ofNullable(resultSet.getBigDecimal("col_name"))
                        .map(BigDecimal::longValue).orElse(null));
    

    在这种情况下,如果SQL值为空,则确保nval为空(而不是零)。

        9
  •  -6
  •   S.L. Barth is on codidact.com Monika Restecka    12 年前

    另一种检查的好方法是,如果您控制了SQL,则在查询本身中为int列添加一个默认值。然后检查这个值。

    例如,对于Oracle数据库,使用nvl

    SELECT NVL(ID_PARENT, -999) FROM TABLE_NAME;
    

    然后检查

    if (rs.getInt('ID_PARENT') != -999)
    {
    }
    

    当然,这也是基于这样一个假设,即列中通常不会找到某个值。