gpt4 book ai didi

java - getObject(index, Long) 为 null 返回 0

转载 作者:太空宇宙 更新时间:2023-11-03 10:35:25 28 4
gpt4 key购买 nike

MySql jdbc:

resultSet.getInt(index)

为 NULL 返回 0,因为它的返回类型是 int,而不是 java.lang.Integer。但是

resultSet.getObject(index, java.lang.Long) 

为空返回 0。它应该返回 NULL。正确的?检查列类型结果集中,列类型为java.lang.Long。它应该返回 null,但为 null 返回 0。

更新

已检查 Oracle 11g:它按预期返回 null。

最佳答案

来自 JDBC 4.3 规范:

When the column value in the database is SQL NULL, it may be returned to the Java application as null, 0, or false, depending on the type of the column value. Column values that map to Java Object types are returned as a Java null; those that map to numeric types are returned as 0; those that map to a Java boolean are returned as false. Therefore, it may be necessary to call the wasNull method to determine whether the last value retrieved was a SQL NULL.

不幸的是措辞含糊,可以理解为getObject如果是数值类型,可能会为NULL返回0,但这不是本意。

JDBC 1.20 规范对此更加清晰:

When you read a SQL “NULL” using one of the ResultSet.getXXX methods, you will receive:

  • A Java “null” value for those getXXX methods that return Java objects.
  • A zero value for getByte, getShort, getInt, getLong, getFloat, and getDouble
  • A false value for getBoolean.

换句话说,getObject 应该为具有 NULL 值的整数列返回 null

getObject(int/String) 的文档进一步支持了这一点(它也适用于 getObject(int/String, Class):

If the value is an SQL NULL, the driver returns a Java null.

关于java - getObject(index, Long) 为 null 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49458017/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com