When using raw JDBC, you can parameterize a PreparedStatement
like so:
PreparedStatement statement = connection.prepareStatement(someSQLString);
String someString = getSomeString();
Integer int = getSomeInteger();
statement.setString(1, someString);
statement.setLong(2, 5L);
statement.setInt(3, int);
...
Here, if someString
is null
, that's fine - strings are nullable. But if getSomeInteger()
returns null
, we have a problem.
PreparedStatement#setInt(int,int)
sets a primitive int
as the value, and therefore cannot be null
.
However, it's perfectly plausible that I might want the value of the 3rd column above to be null
for this particular record. After all, every RDBMS I've ever worked with allows numeric (INT, LONG, etc.) fields to be NULLABLE...
So what's the workaround?
Don't use any of those and use setObject
instead, let the JDBC driver to manage the null
values instead of you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With