首先讓我們創(chuàng)建一個(gè)表。以下是在數(shù)據(jù)庫(kù)’web’中創(chuàng)建表的查詢(xún):
mysql> create table DemoTable ( AdmissionDate date ); Query OK, 0 rows affected (0.53 sec)
登錄后復(fù)制
以下是將空(NULL)java.sql.Date插入MySQL數(shù)據(jù)庫(kù)的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();
}
}
}
登錄后復(fù)制
Java代碼的快照如下:
這將產(chǎn)生以下輸出:
現(xiàn)在讓我們檢查來(lái)自同一表的記錄。快照如下,顯示我們成功插入的空日期:
以上就是在 MySQL 數(shù)據(jù)庫(kù)中插入空 java.sql.Date 的更優(yōu)雅的方法?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!






