I have a very big table in my DataBase. Some of they fields may be null.
I just want to escape from use the:
PreparedStatement.setNull(index, Type.X)
Like:
if(obj.getSomaData() == null){
insertStmt.setNull(++i, java.sql.Types.VARCHAR);
}else{
insertStmt.setString(++i, obj.getSomaData());
}
There's a better and cleaner way to do this?
P.S.: I'm using PostgreSQL.
The primary use case of setNull is for setting primitive types to null, and to handle some edge cases where the driver can't know the data types of the parameters and needs to be explicitly instructed.
In most cases you should be able to use setString(idx, null) (or a setXXX of another object type) just fine.
To quote JDBC 4.2 section 13.2.2.4:
If a Java
nullis passed to any of the setter methods that take a Java object, the parameter will be set to JDBCNULL.
An exception is made for an 'untyped' null (eg using setObject), where it is suggested that for maximum portability you should use setNull or the setObject that takes a type parameter as not all database support that without type information.
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