检查 int 变量是否为 null 或者数据库中为空

发布于 2024-12-05 01:09:53 字数 638 浏览 1 评论 0原文

我有一个变量:

nocustomers = rs.getInt("CUST_TRAN_COUNT");

如果它是否为空,我想执行。

我尝试了

if (nocustomers ==null)

一下,显示错误。

我该如何解决这个问题?


我新修改的代码是:

try{

    query=" select * from SS_summary where txn_date = to_date ('"+asatdate+"','YYYY-MM-DD') ";
    st = conn.createStatement();
    rs = st.executeQuery(query);

    if (rs.next())
    {   


    nocustomers = rs.getInt("CUST_TRAN_COUNT");

    nocheques =rs.getInt("CHEQ_DELIVERED_MAIN");
    }

    if (rs.wasNull()) {
        out.println("NO DATA FOUND");
        }

%>

I am having a variable which is:

nocustomers = rs.getInt("CUST_TRAN_COUNT");

I would like to perform if it is null or not.

I tried

if (nocustomers ==null)

It showed an error.

How do I solve this?


My new modified code is:

try{

    query=" select * from SS_summary where txn_date = to_date ('"+asatdate+"','YYYY-MM-DD') ";
    st = conn.createStatement();
    rs = st.executeQuery(query);

    if (rs.next())
    {   


    nocustomers = rs.getInt("CUST_TRAN_COUNT");

    nocheques =rs.getInt("CHEQ_DELIVERED_MAIN");
    }

    if (rs.wasNull()) {
        out.println("NO DATA FOUND");
        }

%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凡尘雨 2024-12-12 01:09:53

int 不能为 null,因为它是原始类型。

出于同样的原因 ResultSet.getInt() 无法返回 null

您需要调用 ResultSet.wasNull( ) 在您的 getInt() 调用之后检查该值是否为 null

请注意,由于 int 不能为 null,因此即使数据库值为 nullnocustomer 也会有一个值。 getInt() 定义为当 DB 值为 null 时返回 0,因此 nocustomers 将为 0 。

int can't be null, because it's a primitive type.

For the same reason ResultSet.getInt() can't return null.

You will need to call ResultSet.wasNull() right after your getInt() call to check if the value was null.

Note that since int can't be null, nocustomer will have a value even if the database value is null. getInt() is defined to return 0 when the DB value is null, so nocustomers will be 0.

梦年海沫深 2024-12-12 01:09:53

在编译时,java 编译器将抱怨以下消息:

incomparable types: int and <nulltype>
if (nocustomers  == null) {
      ^

那是因为您永远不能对原始类型进行空检查。基本类型分配有默认值(零,对于整数),如果未分配。

如果您想知道读取的值是否为空,请使用 ResultSet.wasNull() 方法(读取整数值后,请参阅提供的 JavaDoc 链接):

nocustomers = rs.getInt("CUST_TRAN_COUNT");
if (rs.wasNull()) {
    nocustomers = -1; //Assuming, -1 means NULL.
}

At compile time, the java compiler will complain with the following message:

incomparable types: int and <nulltype>
if (nocustomers  == null) {
      ^

That is because you can never do a null check on primitive types. Primitive types are assigned with default values (zero, for integers), if unassigned.

If you want to know if the value read was null, use the ResultSet.wasNull() method instead (after reading your integer value, see JavaDoc link provided):

nocustomers = rs.getInt("CUST_TRAN_COUNT");
if (rs.wasNull()) {
    nocustomers = -1; //Assuming, -1 means NULL.
}
剩余の解释 2024-12-12 01:09:53

如果值为NULL,则getInt将返回0,然后您可以调用wasNull来检查它是否包含0或如果它是一个NULL

另请参阅:wasNull()< /a>

If the value was NULL, then 0 will be returned by getInt, and then you could call wasNull to check if it held a 0 or if it was a NULL.

See also: wasNull()

蒗幽 2024-12-12 01:09:53

你可以使用,

object obj = rs.getObject("CUST_TRAN_COUNT");
if (obj != null)
{
rs.getInt("CUST_TRAN_COUNT");
}

但在某些情况下(非常罕见),一旦你调用了 getObject,你就不能调用 getInt,在这种情况下你可以简单地使用

int.parse(obj.toString())

另外,我认为更好的方法是,

  int value = rs.getInt("CUST_TRAN_COUNT");
  boolean nullValue = rs.wasNull();

所以如果数据库返回 null, value 将为 0,但 nullValue 将为 true,以便您可以执行所需的操作

you can use,

object obj = rs.getObject("CUST_TRAN_COUNT");
if (obj != null)
{
rs.getInt("CUST_TRAN_COUNT");
}

but in some cases (very rare) you can't call the getInt once you called the getObject, in that case you can simply use

int.parse(obj.toString())

Also, I think better way to do it is,

  int value = rs.getInt("CUST_TRAN_COUNT");
  boolean nullValue = rs.wasNull();

so if the db returned null, value would be 0 however nullValue will be true so that you can do the needful

热血少△年 2024-12-12 01:09:53

有一个 wasNull ResultSetRowSet 上的 方法:

报告最后读取的列是否具有 SQL NULL 值。请注意,您必须首先对列调用其中一个 getter 方法来尝试读取其值,然后调用方法 wasNull 来查看读取的值是否为 SQL NULL

There is a wasNull method on your ResultSet or RowSet:

Reports whether the last column read had a value of SQL NULL. Note that you must first call one of the getter methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文