【问题标题】:What does resultSet.getString() return for null valueresultSet.getString() 为空值返回什么
【发布时间】:2014-05-12 14:51:11
【问题描述】:

我正在数据库中查询数据库中可能为空的一些值。当我在 resultSet 上调用 getString() 时,如果 DB 值为 null,getString 返回的值是多少?这是javadoc:

Retrieves the value of the designated column in the current row of this ResultSet       
object as a String in the Java programming language.

Parameters:
columnLabel the label for the column specified with the SQL AS clause. If the SQL AS      
clause was not specified, then the label is the name of the column
Returns:
the column value; if the value is SQL NULL, the value returned is null
Throws:
SQLException - if the columnLabel is not valid; if a database access error occurs or    
this    method is called on a closed result set

它说返回值为空,但我不确定这是否意味着“空”或空。我知道我可以让我的代码明显测试这两个值以查看它是否为空,但我不希望它测试不必要的条件。

【问题讨论】:

标签: java db2


【解决方案1】:

它说返回值为空,但我不确定这是“空”还是空

在标准 Java 库中,如果它曾经说“可以返回 null”,则表示引用,也就是 null

但我不希望它测试不必要的条件。

在 java 中,如果您与 null 打交道,通常最好还是检查一下,即使找到 null 的机会很小。

【讨论】:

    【解决方案2】:

    jdbc 在 Windows 10 上使用 mariadb 5.5 - 空列 getString() 返回字符串值“null”,在 rs.getString() 之后立即调用 rs.wasNull() 返回 false。

    注意:我通过查询“... WHERE my-column-name IS NULL”仔细检查了数据库,以确认数据库实际上将其存储为空值。

    因此,虽然生活在象牙塔中并始终信任 javadocs 是件好事,但没有什么比自己编写一个快速测试更好的了。虽然我同意为 null 编写多个测试是低效的,但除非你能保证你的应用程序将在特定的数据库/版本上运行,否则这可能是一个好主意。

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 2019-12-05
      相关资源
      最近更新 更多