Inserting null to an Integer column using JDBC

前端 未结 2 1028
梦谈多话
梦谈多话 2020-11-30 03:53

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

PROTOCOL IN (1, 2, 3)

Also since it is nullabl

相关标签:
2条回答
  • 2020-11-30 04:22

    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.

    0 讨论(0)
  • 2020-11-30 04:29

    In addition to Smit's answer:

    If you want to insert an Integer-object into a database that may be null, you can use

    statement.setObject(4, yourObject, java.sql.Types.INTEGER);
    

    instead of

    if (yourObject == null) {
        statement.setNull(4, java.sql.Types.INTEGER);
    else {
        statement.setInt(4, yourObject);
    }
    
    0 讨论(0)
提交回复
热议问题