我希望有人可以帮助我,我是Java的新手,我需要了解如何使用if语句的结果填充SQL返回的列。
如果结果为真,我需要EUR显示CN *的值,否则只返回金额字段。
if(returnt.getString("ins_type",i).contains("A DRAWDOWN")
|| returnt.getString("ins_type",i).contains("B DRAWDOWN"))
returnt.setDouble("eur", i, -1 * Str.strToDouble(returnt.getString("CN*", i)));
else
returnt.setDouble("eur", i, returnt.getDouble("amount", i));
谢谢, RM
答案 0 :(得分:1)
我想你想写一下eur
专栏。然后你需要一个SQL UPDATE语句。
UPDATE tbl
SET eur = CASE WHEN ... THEN ... ELSE ... END
WHERE ...
而不是executeQuery,executeUpdate。
BTW更好地在Java中使用BigDecimal而不是double,而在SQL DECIMAL中可以安全地使用正确的精度。
转储查询的示例:
String sql = "SELECT ins_type, amount, cn_amount, cn_quantity"
+ " FROM tbl"
+ " WHERE priority > ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInteger(1, priority);
try (ResultSet returnt = stmt.executeQuery()) {
String insType = returnt.getString("ins_type");
double amount = returnt.getDouble("amount");
double cnAmout = returnt.getDouble("cn_amount");
double eur = insType.contains("A DRAWDOWN")
|| insType.contains("B DRAWDOWN")
? -1 * cnAmount
: amount;
// or-operator: || ; if-then-else expression: cond ? a : b
System.out.println("| " + insType + " | " + eur + " |");
} // Ensures returnt.closed() is called for you.
} // Ensures stmt.closed() is called for you.