
- 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
More elegant way to insert empty java.sql.Date in MySQL Database?
Let us first create a table. Following is the query to create a table in database ‘web’ −
mysql> create table DemoTable ( AdmissionDate date ); Query OK, 0 rows affected (0.53 sec)
Here is the Java code to insert empty(NULL) java.sql.Date in MySQL DB −
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertNullDateDemo{ 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 DemoTable(AdmissionDate) values(?) "; ps= con.prepareStatement(query); ps.setNull(1, java.sql.Types.DATE); ps.executeUpdate(); System.out.println("check the table......"); } catch(Exception e){ e.printStackTrace(); } } }
The snapshot of Java code is as follows −
This will produce the following output −
Let us now check records from the same table. The snapshot is as follows displaying empty date we successfully inserted −
- Related Questions & Answers
- What is the most elegant way to check if the string is empty in Python?
- The easiest way to insert date records in MySQL?
- How to insert DECIMAL into MySQL database?
- Insert current date to the database in MySQL?
- Execute INSERT if table is empty in MySQL?
- How to insert a Python tuple in a MySQL database?
- What is the easiest way to store date in MySQL database?
- MySQL query to check if database is empty or not?
- The most elegant way to iterate the words of a C/C++ string
- The most elegant way to iterate the words of a string using C++
- How to check an empty table already in a MySQL database?
- Fastest way to insert with multiple values in a single MySQL query?
- Which one is better to insert NULL or empty string in MySQL?
- How to insert data into a MySQL database with Java?
- Java application to insert null value into a MySQL database?
Advertisements