繁体   English   中英

resultSet.getString() 为空值返回什么

[英]What does resultSet.getString() return for null value

我正在查询数据库中的某些值,这些值在数据库中可能为空。 当我在 resultSet 上调用 getString() 时,如果 DB 值为空,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 库中,如果它说“可以返回 null”,则表示引用,也就是null

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM