gpt4 book ai didi

java - 使用无效的子句组

转载 作者:行者123 更新时间:2023-12-02 08:41:35 24 4
gpt4 key购买 nike

我在尝试对 MySql 数据库表中的一行(记录)分数求和时遇到错误。我想要实现的是更新 term1 表中的 Total_Score 列。如果任何分数发生变化,我必须相应更新 Total_Score 列。以下是给出错误的部分编码。

String qry2 = null;
Prepared statement ps2 = null;

qry2 = " UPDATE term1 SET Total_Score = sum(English + Social_Science + Science + Maths + PE + MAL Arts) WHERE SID = ?";

ps2.=conn.prepareStatement(qry2);
Int res2=ps2.executeUpdate();

最佳答案

该代码中有很多错误:

String qry2 = null;
Prepared statement ps2 = null; // Data type is `PreparedStatement`, not `Prepared statement`

// Don't call `sum()` unless you're summing multiple rows, and you're not.
// What is `MAL Arts`? Column names cannot have spaces, unless you quote
// the name, and you really don't want to be doing that.
qry2 = " UPDATE term1 SET Total_Score = sum(English + Social_Science + Science + Maths + PE + MAL Arts) WHERE SID = ?";

ps2.=conn.prepareStatement(qry2); // no period before =
Int res2=ps2.executeUpdate(); // Data type is `int`, not `Int`

看起来你的代码应该是:

String qry2 = "UPDATE term1" +
" SET Total_Score = English + Social_Science + Science + Maths + PE + MALArts" +
" WHERE SID = ?";
int res2;
try (PreparedStatement ps2 = conn.prepareStatement(qry2)) {
ps2.setInt(1, sid);
res2 = ps2.executeUpdate();
}
// code using `res2` here

关于java - 使用无效的子句组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61356101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com