
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Java application to insert null value into a MySQL database?
To set null value with Java, the statement is as follows −
ps.setNull(yourIndex, Types.NULL);
Let us first create a table −
mysql> create table DemoTable1893 ( FirstName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
The Java code is as follows −
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Types; public class InsertNullValueIntoDatabase{ public static void main(String[] args){ Connection con=null; PreparedStatement ps=null; try{ con=DriverManager.getConnection("jdbc:mysql://localhost:3306/web?useSSL=false", "root","123456"); String query="insert into DemoTable1893(FirstName) values(?) "; ps= con.prepareStatement(query); ps.setNull(1, Types.NULL); ps.executeUpdate(); System.out.println("Check the DemoTable1893 "); } catch(Exception e){ e.printStackTrace(); } } }
This will produce the following output −
Check the MySQL table now using select statement −
mysql> select * from DemoTable1893;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | NULL | +-----------+ 1 row in set (0.00 sec)
- Related Articles
- Insert NULL value into database field with char(2) as type in MySQL?
- Insert NULL value into INT column in MySQL?
- How to insert data into a MySQL database with Java?
- Insert default into not null column if value is null in MySQL?
- How to insert DECIMAL into MySQL database?
- How to insert DATE into a MySQL column value using Java?
- How to insert NULL into char(1) in MySQL?
- How do I insert a NULL value in MySQL?
- How do we insert/store a file into MySQL database using JDBC?
- How to get the id after INSERT into MySQL database using Python?
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- How to insert an image in to MySQL database using Java program?
- Select some data from a database table and insert into another table in the same database with MySQL
- How to insert a Python tuple in a MySQL database?
- How to insert/store JSON array into a database using JDBC?

Advertisements