PreparedStatement stmt = conn.prepareStatement("INSERT INTO users (name, age) VALUES (?,?)"); stmt.setString(1, "John"); stmt.setNull(2, Types.INTEGER); stmt.executeUpdate();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM products WHERE price > ?"); stmt.setNull(1, Types.DOUBLE); ResultSet rs = stmt.executeQuery();In both examples, the setNull method is used to set a NULL value for a parameter. The first example sets the second parameter to NULL, while the second example sets the first parameter to NULL using the Types.DOUBLE data type. Overall, the java.util package library provides several useful classes and interfaces for Java developers, with the PreparedStatement class being one of them. The setNull method in this class can be used to handle NULL values in a convenient way.