constantlearner constantlearner - 1 year ago 64
Java Question

JDBC:Inserting null to Integer column

I have an sql column PROTOCOL it is nullable and a constraint on the table

PROTOCOL IN (1, 2, 3)

Also since it is nullable I want to set and get null values into table

But I cannot do setInt and getInt as null .How to set a null to column as null from jdbc



Answer Source

Try using.

   pst.setNull(4, java.sql.Types.INTEGER);  //pst is prepared statement instance.

Interface PreparedStatement.setNull API

Mapping of java.sql.Types to SQL types

P.S. : Edited to reflect Java 8 updates.