constantlearner
constantlearner

Reputation: 5247

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. How to get the value if its null? I can do rs.getInt() but I dont think it returns null?

if(protocol==0)
            {

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

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

Upvotes: 0

Views: 442

Answers (2)

Andreas Fester
Andreas Fester

Reputation: 36630

I can do rs.getInt() but I dont think it returns null?

Use ResultSet.wasNull() after getInt() to check if the last column read was NULL.

Or, use ResultSet.getObject() instead of getInt(), which returns null if the column is NULL.

Upvotes: 3

SJuan76
SJuan76

Reputation: 24885

Use wasNull() method.

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

Upvotes: 4

Related Questions