Home > Database > Mysql Tutorial > How to Insert NULL Values into an Integer Column Using JDBC?

How to Insert NULL Values into an Integer Column Using JDBC?

Mary-Kate Olsen
Release: 2024-12-26 09:23:15
Original
413 people have browsed it

How to Insert NULL Values into an Integer Column Using JDBC?

Inserting Null Values to an Integer Column with JDBC

When working with nullable SQL columns that have constraints, setting null values can be a challenge. In particular, the setInt method cannot be used to set null values.

To insert a null value into an integer column using JDBC, follow these steps:

  1. Prepare the statement: Obtain a PreparedStatement instance, either through a connection or a statement factory.
  2. Set the null value: Use the setNull method on the PreparedStatement. The first argument is the parameter index (starting from 1), and the second argument specifies the SQL type to be used for the null value. For an integer column, use java.sql.Types.INTEGER.
Example:
PreparedStatement pst = connection.prepareStatement("INSERT INTO table_name (PROTOCOL) VALUES (?)");
pst.setNull(1, java.sql.Types.INTEGER);
Copy after login
  1. Execute the statement: Call the executeUpdate or execute method on the PreparedStatement to execute the SQL statement.

This approach allows you to explicitly set null values into nullable SQL columns, even when there are constraints on the column values.

The above is the detailed content of How to Insert NULL Values into an Integer Column Using JDBC?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template