我必须使用case
语句更新列,但如何在PreparedStatement
语句上设置case
参数。我的代码看起来像这样:
PreparedStatement updateAkaunAmanahPs = con.prepareStatement(""
+ "UPDATE GL_AKAUN_AMANAH "
+ " SET AMAUN_YOBCR = (CASE WHEN AMAUN_YOBCR IS NOT NULL THEN AMAUN_YOBCR = AMAUN_YOBCR + ? ELSE ? END) "
+ " WHERE PKID = ?");
updateAkaunAmanahPs.setBigDecimal(1, ytddr.subtract(ytdcr));
// if the case statement is true, how about this parameter ?
updateAkaunAmanahPs.setBigDecimal(2, ytddr.subtract(ytdcr));
updateAkaunAmanahPs.setLong(3, 30);
有什么想法吗?
答案 0 :(得分:2)
您只需要考虑占位符的数量(?
),在您的情况下,您有3个占位符,然后您需要相应地设置它们(以相同的顺序)
updateAkaunAmanahPs.setBigDecimal(1, ytddr.subtract(ytdcr)); // for first placeholder
updateAkaunAmanahPs.setBigDecimal(2, ytddr.subtract(ytdcr)); // for second
updateAkaunAmanahPs.setLong(3, 30); // for third