简体   繁体   English

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

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

I'm querying the DB for some values that can potentially be null in the DB.我正在查询数据库中的某些值,这些值在数据库中可能为空。 When the I call getString() on the resultSet what will be the value returned by getString if the DB value is null?当我在 resultSet 上调用 getString() 时,如果 DB 值为空,getString 返回的值是多少? Here's the javadoc:这是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

It says the returned value is null, but I'm not sure if this means "null" or null.它说返回的值为空,但我不确定这是否意味着“空”或空。 I know I can have my code obviously test for both values to see that it is null, but I would prefer it not test for unnecessary conditions.我知道我可以让我的代码明显地测试这两个值以查看它是否为空,但我不希望它测试不必要的条件。

It says the returned value is null, but I'm not sure if this means "null" or null它说返回的值为空,但我不确定这是否意味着“空”或空

In the standard java libraries if it ever says "can return null", it means the reference, aka null .在标准 Java 库中,如果它说“可以返回 null”,则表示引用,也就是null

but I would prefer it not test for unnecessary conditions.但我不希望它测试不必要的条件。

In java if your dealing with null its normally better off to check anyway even if the chance of finding a null is remotely small.在 java 中,如果您处理null通常最好还是检查一下,即使找到null的机会很小。

jdbc using mariadb 5.5 on Windows 10 - null column getString() returns the String value "null", and a call to rs.wasNull() immediately after the rs.getString() returns false. jdbc 在 Windows 10 上使用 mariadb 5.5 - null 列getString()返回字符串值“null”,并且在rs.getString()返回 false 后立即调用rs.wasNull()

Note: I double checked the database with a query " ... WHERE my-column-name IS NULL" to confirm the database is actually storing it as a null.注意:我使用查询“... WHERE my-column-name IS NULL”仔细检查了数据库,以确认数据库实际上将其存储为空值。

So while it's nice to live in an ivory tower and always trust the javadocs, nothing beats writing a quick test yourself.因此,虽然住在象牙塔中并始终信任 javadoc 是件好事,但没有什么比自己编写快速测试更合适的了。 And though I agree coding multiple tests for null is inefficient, unless you can guarantee the specific database/version your app will be running on, it's probably a good idea.虽然我同意为 null 编写多个测试是低效的,除非你能保证你的应用程序将在特定的数据库/版本上运行,否则这可能是一个好主意。

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

相关问题 需要更改ResultSet.getString(Value) - 有条件 - Need to change ResultSet.getString(Value)-— conditionally JDBC-ODBC问题-当有值时,Resultset.getString()返回null - JDBC-ODBC issue - Resultset.getString() returning null when there is a value resultSet.getString(“ TABLE_CAT”)-什么是TABLE_CAT? - resultSet.getString(“TABLE_CAT”) - what is TABLE_CAT? 如果我通过传递blob使用resultet.getString()会发生什么? - What could happen if I use resultset.getString() by passing a blob? 将 ResultSet.getString 值与来自文本字段的输入进行比较 - Compare ResultSet.getString value to an input from textfield Oracle ResultSet.getString(“ colname”)返回字符串“ null”而不是java null - Oracle ResultSet.getString(“colname”) returning the string “null” instead of java null 如何在Java中将Resultset.getString()与String进行比较? - How to compare Resultset.getString() to String in Java? 是什么导致Java程序不同地对待两个ResultSet.getString()方法? - What is causing Java program to treat the two ResultSet.getString() methods differently? JDBC ResultSet.getString ResultSet异常的开始/结束之前/之后 - JDBC ResultSet.getString Before/After Start/End of ResultSet Exception 使用ResultSet.getString()或ResultSet.getTimestamp()以获得时间戳更好吗? - Is it better to use ResultSet.getString() or ResultSet.getTimestamp() to get a timestamp?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM