简体   繁体   English

如何在具有CASE语句的SQL中使用PreparedStatement

[英]How to use PreparedStatement in SQL having CASE statement

I have to update a column using case statement, but how do I set a PreparedStatement parameter over case statement. 我必须使用case语句更新列,但是如何在case语句上设置PreparedStatement参数。 My code look like this: 我的代码如下所示:

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);

Any ideas ? 有任何想法吗 ?

You just have to consider the number of placeholder ( ? ), in your case, you have 3 placeholders, then you need to set them accordingly ( in same order ) 您只需要考虑占位符的数量( ? ),就您的情况而言,您有3个占位符,那么您需要相应地设置它们( 以相同的顺序

updateAkaunAmanahPs.setBigDecimal(1, ytddr.subtract(ytdcr)); // for first placeholder
updateAkaunAmanahPs.setBigDecimal(2, ytddr.subtract(ytdcr)); // for second
updateAkaunAmanahPs.setLong(3, 30); // for third 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM