Java로 null 값을 설정하는 명령문은 다음과 같습니다. -
ps.setNull(yourIndex, Types.NULL);
먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1893 ( FirstName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
자바 코드는 다음과 같습니다 -
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();
}
}
} 이것은 다음과 같은 출력을 생성합니다 -
이제 select 문을 사용하여 MySQL 테이블을 확인하십시오 -
mysql> select * from DemoTable1893;
이것은 다음과 같은 출력을 생성합니다 -

+-----------+ | FirstName | +-----------+ | NULL | +-----------+ 1 row in set (0.00 sec)