0

When setting a null value, Should we always use the value java.sql.Types.NULL? Does the following code work for all types?:

preparedStatement.setNull(index, java.sql.Types.NULL);

Why would someone choose to use another type like so?:

preparedStatement.setNull(index, java.sql.Types.INTEGER);
Sch
  • 1
  • 1
  • 1
    Does `preparedStatement.setNull(index, java.sql.Types.NULL);` work for any types? The spec says it needs to be the type of the column, but different drivers may handle things differently. – Kayaman Aug 25 '23 at 11:00
  • Some database drivers might be picky, or some database drivers might not support `Types.NULL`, or in some database drivers it might be more efficient to use `setNull(1, Types.BOOLEAN` vs `setNull(1. Types.VARCHAR)`. The thing to keep in mind is that JDBC is an **API**, and although a lot of things are specified, there is enough room for differences between drivers (either because the specification gives that room, or because of misinterpretation of the specification, or because a driver implementation runs into limitations of the database they're talking to). – Mark Rotteveel Aug 25 '23 at 11:32

0 Answers0