Java 无效使用条款组

Java 无效使用条款组,java,mysql,netbeans,Java,Mysql,Netbeans,我在尝试对MySql数据库表中的一行(记录)中的分数求和时出错。我试图实现的是更新term1表中的Total_Score列。如果a中有任何分数的变化,那么我必须相应地更新Total_Score列。下面是给出错误的部分编码 String qry2 = null; Prepared statement ps2 = null; qry2 = " UPDATE term1 SET Total_Score = sum(English + Social_Science + Science + Maths

我在尝试对MySql数据库表中的一行(记录)中的分数求和时出错。我试图实现的是更新
term1
表中的
Total_Score
列。如果a中有任何分数的变化,那么我必须相应地更新
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

这是您的确切代码吗?或者您可以有一个,而不需要
update
语句。修复损坏的架构。数据库表不是电子表格。在标准化环境中,您将不会为单独的术语使用单独的表,也不会为单独的主题使用单独的列。你可能会有一个学生专栏,一个学期专栏,一个学科专栏,还有一个马克专栏。这会忽略房间里的大象。!