preparedStatement.setInt(1, pimDataVo.getItemFlashMessageId());
preparedStatement.setInt(2, pimDataVo.getErpImprintCodeId());
preparedStatement.setInt(3, pimDataVo.getPublisherCodeId());
preparedStatement.setInt(4, pimDataVo.getGlClassId());
如果get值为零,有没有办法将这些值设置为null。所有都是数字列
答案 0 :(得分:9)
是的,您需要使用setNull
方法。所以,在你的情况下,它将是:
if (pimDataVo.getItemFlashMessageId() != 0) {
preparedStatement.setInt(1, pimDataVo.getItemFlashMessageId());
} else {
// use setNull
preparedStatement.setNull(1, java.sql.Types.INTEGER);
}
您对其他值使用类似的方法。你也可以写一个HELPER CLASS来执行此操作(如果适合你)(所以你不要重复很多代码)。像这样:
public static void setIntOrNull(PreparedStatement pstmt, int column, int value)
{
if (value != 0) {
pstmt.setInt(column, value);
} else {
pstmt.setNull(column, java.sql.Types.INTEGER);
}
}
然后你使用这样的代码:
Helper.setIntOrNull(preparedStatement, 1, pimDataVo.getItemFlashMessageId());
Helper.setIntOrNull(preparedStatement, 2, pimDataVo.getErpImprintCodeId());
Helper.setIntOrNull(preparedStatement, 3, pimDataVo.getPublisherCodeId());
Helper.setIntOrNull(preparedStatement, 4, pimDataVo.getGlClassId());