Reputation: 31
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.
Upvotes: 3
Views: 1963
Reputation: 109000
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
null
is 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.
Upvotes: 6