Java কোড: setNull() মেথড
ps.setNull(yourIndex, Types.NULL);
টেবিল তৈরি
mysql> create table DemoTable1893 ( FirstName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
সম্পূর্ণ Java প্রোগ্রাম
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();
}
}
}
MySQL-এ যাচাই
mysql> select * from DemoTable1893;
+-----------+ | FirstName | +-----------+ | NULL | +-----------+ 1 row in set (0.00 sec)

ব্যাখ্যা: PreparedStatement-এ প্লেসহোল্ডার (?) থাকে। setNull(parameterIndex, sqlType) দিয়ে সেই পজিশনে NULL বাইন্ড করা হয়। Types.NULL হলো java.sql.Types ক্লাসের কনস্ট্যান্ট। এটি সরাসরি NULL লিটারেল লেখার চেয়ে টাইপ-সেফ এবং SQL ইনজেকশন থেকে রক্ষা করে।