Home >Database >Mysql Tutorial >How to Insert NULL Values into a Constrained Integer Column using JDBC?
Inserting Null to an Integer Column using JDBC
Suppose you have a SQL column named PROTOCOL that is nullable and has the following constraint:
PROTOCOL IN (1, 2, 3)
You want to insert and retrieve null values into this column using JDBC.
The standard methods setInt() and getInt() cannot handle null values directly. Instead, you can use the setNull() method to explicitly set a column to null.
Syntax:
pst.setNull(columnIndex, sqlType)
Example:
// Get the prepared statement instance PreparedStatement pst = connection.prepareStatement("INSERT INTO table_name (PROTOCOL) VALUES (?)"); // Set the PROTOCOL column to null pst.setNull(4, java.sql.Types.INTEGER); // Execute the statement pst.executeUpdate();
By using setNull(), you can set null values into integer columns even if they have constraints.
The above is the detailed content of How to Insert NULL Values into a Constrained Integer Column using JDBC?. For more information, please follow other related articles on the PHP Chinese website!