首先让我们创建一个表。以下是在数据库'web'中创建表的查询:
mysql> create table DemoTable ( AdmissionDate date ); Query OK, 0 rows affected (0.53 sec)
以下是将空(NULL)java.sql.Date插入MySQL数据库的Java代码 −
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();
}
}
}Java代码的快照如下:
这将产生以下输出:
现在让我们检查来自同一表的记录。快照如下,显示我们
成功插入的空日期:








