典型情况:我们有一些类型为Integer
的可为空的列,它可能是null
或某些int
值。因此,我们使用以下命令:将null设置为PreparedStatement
private static void setIntOrNull(PreparedStatement stmt, int col, Integer i)
throws SQLException
{
if (i == null)
stmt.setNull(col, java.sql.Types.INTEGER);
else
stmt.setInt(col, i);
}
但对于我这种情况是怎么样的坏习惯 - 到内部空隙更改方法中的外部对象(reffering到Robert Martin's "Clean Code"第17章:气味和启发,功能,F2)。我尽量避免这种情况,但是这次我找不到更好的解决方案。也许有人可以帮我一个吗?
+1。我从来没有理解为什么PreparedStatement中没有'setInteger(int position,Integer valueOrNull)'方法。为什么ResultSet中没有'Integer getNullableInteger(int position)'。 –