Home > Database > Mysql Tutorial > More elegant way to insert empty java.sql.Date in MySQL database?

More elegant way to insert empty java.sql.Date in MySQL database?

WBOY
Release: 2023-09-07 16:21:05
forward
1404 people have browsed it

First let us create a table. The following is the query to create a table in the database 'web':

mysql> create table DemoTable
(
   AdmissionDate date
);
Query OK, 0 rows affected (0.53 sec)
Copy after login

The following is the Java code to insert an empty (NULL) java.sql.Date into the MySQL database −

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();
      }
   }
}
Copy after login

Snapshot of the Java code As follows:

在 MySQL 数据库中插入空 java.sql.Date 的更优雅的方法?

在 MySQL 数据库中插入空 java.sql.Date 的更优雅的方法?

# This will produce the following output:

在 MySQL 数据库中插入空 java.sql.Date 的更优雅的方法?

Now let us check the records from the same table. The snapshot is below, showing the empty date we successfully inserted:

在 MySQL 数据库中插入空 java.sql.Date 的更优雅的方法?

The above is the detailed content of More elegant way to insert empty java.sql.Date in MySQL database?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template