Tuesday, February 26, 2008

How to handle non-nullable fields in database while insert/update through JDBC.

Suppose if we have a non-nullable field in database table and while inserting/updating through jdbc how to ensure a null value isn't passed to it?

Using a ternary operator we can check if the value that we are trying to insert is null. If yes then pass an empty string or else pass the orginal value.

objPreparedStmt.setString(7, null == objRequestDTO.getUserId() ? ABCApplicationConstants.COMMON_EMPTYSTRING : objRequestDTO.getUserId());