检查 Java ResultSet 中是否存在 null int 值
在 Java 中,我尝试测试 ResultSet 中的空值,其中列被转换为原始 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() 测试是多余的吗?
教育我们,谢谢
In Java I'm trying to test for a null value, from a ResultSet, where the column is being cast to a primitive int type.
int iVal;
ResultSet rs = magicallyAppearingStmt.executeQuery(query);
if (rs.next()) {
if (rs.getObject("ID_PARENT") != null && !rs.wasNull()) {
iVal = rs.getInt("ID_PARENT");
}
}
From the code fragment above, is there a better way to do this, and I assume that the second wasNull() test is redundant?
Educate us, and Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
当字段值为
NULL
时,ResultSet.getInt
默认返回0
,这也是iVal 的默认值声明。在这种情况下,您的测试是完全多余的。
如果您实际上想要在字段值为 NULL 时做一些不同的事情,我建议:(
编辑为下面的 @martin 注释;编写的 OP 代码将无法编译,因为
iVal
未初始化)The default for
ResultSet.getInt
when the field value isNULL
is to return0
, which is also the default value for youriVal
declaration. In which case your test is completely redundant.If you actually want to do something different if the field value is NULL, I suggest:
(Edited as @martin comments below; the OP code as written would not compile because
iVal
is not initialised)另一个解决方案:
Another solution:
只需检查该字段是否为
null
或不使用ResultSet#getObject()
。将-1
替换为您想要的任何 null 值。或者,如果您可以保证使用正确的数据库列类型,以便
ResultSet#getObject()
真正返回Integer
(因此不是Long
>、Short
或Byte
),那么您也可以将其类型转换为Integer
。Just check if the field is
null
or not usingResultSet#getObject()
. Substitute-1
with whatever null-case value you want.Or, if you can guarantee that you use the right DB column type so that
ResultSet#getObject()
really returns anInteger
(and thus notLong
,Short
orByte
), then you can also just typecast it to anInteger
.我认为,这是多余的。
rs.getObject("ID_PARENT")
应返回Integer
对象或null
(如果列值实际上为NULL
) >。所以甚至应该可以做类似的事情:I think, it is redundant.
rs.getObject("ID_PARENT")
should return anInteger
object ornull
, if the column value actually wasNULL
. So it should even be possible to do something like:你也可以简单地使用。
AFAIK即使它是 NULL
AFAIK you can simply use
even if it is NULL.
只是 Java 泛型的更新。
您可以创建一个实用程序方法来从先前转换的给定 ResultSet 中检索任何 Java 类型的可选值。
不幸的是, getObject(columnName, Class) 不返回 null,而是给定 Java 类型的默认值,因此需要 2 次调用
在此示例中,您的代码可能如下所示:
很高兴获得反馈
Just an update with Java Generics.
You could create an utility method to retrieve an optional value of any Java type from a given ResultSet, previously casted.
Unfortunately, getObject(columnName, Class) does not return null, but the default value for given Java type, so 2 calls are required
In this example, your code could look like below:
Happy to get feedback
您可以使用 resultSet 和 Number 类型的列名来调用此方法。它将返回 Integer 值或 null。数据库中的空值不会返回零
You can call this method using the resultSet and the column name having Number type. It will either return the Integer value, or null. There will be no zeros returned for empty value in the database
如果您想要调用
ResultSet.wasNull()
的替代方法,您可以使用getObject()
并强制转换为正确的类型。您还可以使用
setObject()
在Statement
中设置 null 值。If you want an alternative to calling
ResultSet.wasNull()
you can usegetObject()
and cast to the correct type.You can also set null values in a
Statement
withsetObject()
.为了方便起见,您可以围绕 ResultSet 创建一个包装类,该类在
ResultSet
通常不会返回 null 值时返回 null 值。For convenience, you can create a wrapper class around ResultSet that returns null values when
ResultSet
ordinarily would not.以防万一有人在 Kotlin 编程时来到这里(就像我所做的那样),BalusC 建议的答案很好。请注意,
Short
和Float
在ResultSet< 中分别实例化为
Integer
和Double
/code>,我们应该在调用getObject()
后将它们转换为正确的类型。就我而言,最终代码是:Just in case someone comes here while programming in Kotlin (as I did), the answer suggested by BalusC works fine. Just be aware that
Short
andFloat
are instantiated asInteger
andDouble
(respectively) insideResultSet
, and we should cast them to the correct type after callinggetObject()
. In my case the final code was:在 Kotlin 中,我只会解决一次,然后永远解决这个问题:
所以稍后你可以这样做:
我想如果你愿意你也可以这样做
所以你可以这样做:
或者
编辑:如果图书馆仍然很挑剔,你终于可以这样做:
In Kotlin I would just solve it once and be done with the issue forever with this:
So later you can just do this:
I guess if you want you could also do this too
So you can just do this:
Or better still make both methods available if you happen to be dealing with developers that can't agree on even the simplest of things.
Edit: if the library is still being fussy you can finally do something like:
使用 java 8,您可以这样做:
在这种情况下,如果 SQL 值为 NULL,则确保 nVal 将为 null(而不是零)
With java 8 you can do this:
In that case you ensure that the nVal will be null (and not zero) if the SQL value is NULL
如果您可以控制 SQL,另一种很好的检查方法是在查询本身中为 int 列添加默认值。然后只需检查该值即可。
例如,对于 Oracle 数据库,使用 NVL
然后检查
当然,这也是假设有一个通常不会在列中找到的值。
Another nice way of checking, if you have control the SQL, is to add a default value in the query itself for your int column. Then just check for that value.
e.g for an Oracle database, use NVL
then check
Of course this also is under the assumption that there is a value that wouldn't normally be found in the column.