

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Insert NULL value into INT column in MySQL?
- Insert NULL value into database field with char(2) as type 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?
- Insert JSON into a MySQL table?
- How to insert/store JSON array into a database using JDBC?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- What MySQL returns if I insert invalid value into ENUM?
Advertisements