JAVA的RESULTSET中如何判断一条记录的一个字段的值为NULL?类似.NET的
rs("aaa")==DBNull.Value

解决方案 »

  1.   

    一般用rs.getXXX("columnName");取得value.
    java的变量有primitive(原始)类型和非原始类型。
    原始类型包括boolean,byte,char,double,float,int,long,short.
    这些如果ResultSet里的COLUMN为NULL的话,赋予初始值,呵呵。这个具体初始值是什么我就不1说 了
    其他为非原始类型,例如:String, time, date, timestamp.....
    这个如果为NULL的话,取得的value也是NULL。
    可以用rs.getXXX("columnName")==null判断。
      

  2.   

    是啊是啊。不过有一个方法可以帮你解决。
    你先rs.getXXX("columnName")==null判断。如果不是的话再用
    rs.warNull()来判断,为什么不直接用这个,请看API。
    wasNull
    public boolean wasNull()
                    throws SQLExceptionReports 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. Returns:
    true if the last column value read was SQL NULL and false otherwise 
    Throws: 
    SQLException - if a database access error occurs
      

  3.   

    要确定给定结果值是否是JDBC NULL,必须先读取该列,然后使用ResultSet.wasNull方法检查该次读取是否返回JDBC NULL。例如:
          rs = stmt.executeQuery("select id,type_id,prod_name from product where id=1");
          //此次假设type_id列为Null值
          System.out.println("id=" + rs.getInt("id");
          System.out.println("type_id=" + rs.getInt("type_id");
          if (rs.wasNull()) {
            System.out.println("type_id was null!";
          }
      

  4.   

    type_id是int的时候,
    如果type_id列为Null,用rs.getInt读出来的值为0,但是rs.wasNull()==true
    如果type_id列为0,用rs.getInt读出来的值也为0.但是rs.wasNull()==false