【问题标题】:Handling the null value from a resultset处理结果集中的空值
【发布时间】:2011-08-24 21:12:24
【问题描述】:

我目前返回了一个结果集,其中一列中的字符串值可能为空(我的意思是根本没有值)。我有一个条件来实现如下

rs = st.executeQuery(selectSQL);
output = rs.getString("column");

由于该列在数据库中可能为空,当该列为空时,rs.getString() 将抛出 NullPointerException。如果列为空,我希望输出为空字符串,如output = "";。我也无法检查if(rs.getString("column) != null。我该如何应对这种情况?

我真正的问题:

try {
    rs = st.executeQuery(sql);
    int i = 0;
    while (rs.next()) {
        output[i] = rs.getString(column);
        // column field in the database contains multiple results, but sometimes
        // may be null
        i++;
    }
} catch (SQLException e) {
    e.printStackTrace();
    // other than tracing the exception i want to fill the array too
}
return output;

现在,如果其中一个列值不包含任何值,即 null,我希望将 output[i] 定义为 N/A。这个问题源于column 字段在数据库中允许为NULL。很抱歉告诉你这是一个 NPE,而实际上它是一个 SQLException

【问题讨论】:

  • 如果该列为 null,则 ResultSet 应返回 null。你确定 ResultSet 不为空吗?
  • 通常可以调用wasNull()方法。我不明白为什么会在这里抛出 NPE。
  • while(rs.next()){ rs.getX(); }
  • 我很好奇为什么这被否决了。

标签: java jdbc


【解决方案1】:

你可以简单地使用-

rs = st.executeQuery(selectSQL);
output = rs.getString("column");
if(!output.isEmpty()) {
 //
}

在 MySQL 中遇到问题-

output!=null
output!=""

但 output.isEmpty() 对 rs.getString() 有效。

【讨论】:

  • output 为空时,这将抛出NullPointerException
【解决方案2】:

我遇到了同样的问题。但我相信,在 sql 中处理 null 并不是一个好的选择。这些事情应该在java程序中处理以获得更好的性能。 其次, rs.getString("column") != NULL 也不是一个好的选择,因为您正在比较字符串的引用而不是值。在检查 null 或 isEmpty() 方法时最好使用 .equals() 方法。同样,有了这个你可以使用空检查,这很好。

【讨论】:

    【解决方案3】:

    代码应该如下所示

    String selectSQL = "SELECT IFNULL(tbl.column, \"\") AS column FROM MySQL_table AS tbl";
    Statement st = ...;
    Result set rs = st.executeQuery(selectSQL);
    

    【讨论】:

      【解决方案4】:

      由于该列在 数据库,rs.getString() 将 抛出 NullPointerException()

      没有。

      如果列存在于选定的结果集中(SELECT 查询列),rs.getString 将不会抛出 NullPointer 对于特定记录,如果 'comumn 的值在 db 中为空,您必须执行以下操作 -

      String myValue = rs.getString("myColumn");
      if (rs.wasNull()) {
          myValue = ""; // set it to empty string as you desire.
      }
      

      您可能需要参考wasNull() 文档 -

      From java.sql.ResultSet
      boolean wasNull() throws SQLException;
      
      * Reports whether
      * the last column read had a value of SQL <code>NULL</code>.
      * Note that you must first call one of the getter methods
      * on a column to try to read its value and then call
      * the method <code>wasNull</code> to see if the value read was
      * SQL <code>NULL</code>.
      *
      * @return <code>true</code> if the last column value read was SQL
      *         <code>NULL</code> and <code>false</code> otherwise
      * @exception SQLException if a database access error occurs or this method is 
      *            called on a closed result set
      */
      

      【讨论】:

      • 非常感谢 d-live.. 这解决了我的问题.. 克里斯指导我它不可能是 NPE(所以我再次检查它不是).. .Peter 向我提出了一个与您类似的建议,但我以前无法理解他。但我现在这样做了 :-) 感谢所有帮助我的人......我真的不知所措,这里的人回复如此之快......我可以在几分钟内解决问题。
      • 很高兴它解决了您的问题!请接受您认为合适的答案!
      • 为什么@javaNOob 5 年都没有选择这个答案?
      • @CaioIglesias 因为这似乎是他唯一的问题,他在问题解决后离开了......什么,显然不是正确的做法......
      【解决方案5】:

      要在数据库中字段为空时处理验证,您可以添加以下条件。

      String name = (oRs.getString ("name_column"))! = Null? oRs.getString ("name_column"): "";
      

      您可以通过此验证字段何时为空并且不标记异常。

      【讨论】:

        【解决方案6】:

        String 为 null 是一个很好的机会,但是当您在表中看到值时,ResultSet 却打印了 null,这可能意味着在使用 ResultSet 的值之前连接已关闭。

        Class.forName("org.sqlite.JDBC");
        con = DriverManager.getConnection("jdbc:sqlite:My_db.db");
        String sql = ("select * from cust where cust_id='" + cus + "'");
        pst = con.prepareStatement(sql);
        rs = pst.executeQuery();
        con.close();
        System.out.println(rs.getString(1));
        

        即使有值也会打印null

        Class.forName("org.sqlite.JDBC");
        con = DriverManager.getConnection("jdbc:sqlite:My_db.db");
        String sql = ("select * from cust where cust_id='" + cus + "'");
        pst = con.prepareStatement(sql);
        rs = pst.executeQuery();
        System.out.println(rs.getString(1));
        con.close();
        

        如果表中有值,则不会打印null

        【讨论】:

          【解决方案7】:

          我能够做到这一点:

          String a;
          if(rs.getString("column") != null)
          {
              a = "Hello world!";
          }
          else
          {
              a = "Bye world!";
          }
          

          【讨论】:

          • 效果很好,但不适用于 rs.getInt() 因为它返回一个原语(默认为 0)。应该使用 rs.wasNull
          • @BojanPetkovic,这个问题专门询问getString。您的回复基于getInt
          【解决方案8】:

          getString() 方法的描述如下:

           the column value; if the value is SQL NULL, the value returned is null
          

          这意味着您的问题不在于 String 值为空,而在于其他一些 对象是,可能是您的 ResultSet 或者您关闭了连接或其他东西 像这样。提供堆栈跟踪,这会有所帮助。

          【讨论】:

          • 是的,完美...我想你明白我想说什么。由于 rs.getString(col) 在 column 为 infact 数据库 null 时返回 null,因此会立即引发异常。我想检查当列是数据库空(即getString()返回空)时,输出[i]被分配N/A(但对我来说空字符串“”也是可以接受的)
          • 对于 rs.getString(),您可以使用 null 比较,但是当您有不允许 null 的数据库列(例如 rs.getInt())时,您必须使用 rs.wasNull ()。
          【解决方案9】:
          output = rs.getString("column");// if data is null `output` would be null, so there is no chance of NPE unless `rs` is `null`
          
          if(output == null){// if you fetched null value then initialize output with blank string
            output= "";
          }
          

          【讨论】:

          • 这是一个比上述“rs.wasNull()”更好的答案,它并不适用于所有情况。
          • 这行得通!谢谢! if rs.getString("column") == null {//whatever}
          猜你喜欢
          • 2011-10-16
          • 1970-01-01
          • 1970-01-01
          • 2013-06-28
          • 2023-04-07
          • 2015-12-27
          • 1970-01-01
          • 2011-01-31
          • 2018-08-11
          相关资源
          最近更新 更多