简体   繁体   English

如何从表中获取jdbc空对象

[英]how to get jdbc null object from table

I have an sql column PROTOCOL of Number type .It is nullable and a constraint on the table PROTOCOL IN(1,2,3).I am able to set to null. 我有一个Number类型的SQL列PROTOCOL。它可以为空,并且对表PROTOCOL IN(1,2,3)有约束。我可以将其设置为null。 How to get the value if its null? 如果该值为空,如何获取? I can do rs.getInt() but I dont think it returns null? 我可以做rs.getInt(),但我不认为它返回null吗?

if(protocol==0)
            {

               stmt.setNull(15, java.sql.Types.INTEGER);                

           }
            else{
            stmt.setInt(15, protocol);
            }

Use wasNull() method. 使用wasNull()方法。

 Integer myValue = rs.getInt(15);
 if (rs.wasNull()) {
   myValue = null;
 }

I can do rs.getInt() but I dont think it returns null? 我可以做rs.getInt(),但我不认为它返回null吗?

Use ResultSet.wasNull() after getInt() to check if the last column read was NULL. getInt() ()之后使用ResultSet.wasNull()检查最后读取的列是否为NULL。

Or, use ResultSet.getObject() instead of getInt() , which returns null if the column is NULL . 或者,使用ResultSet.getObject()代替getInt() ,如果getInt() NULL ,则该方法返回null

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM