Java& SQL:ExecuteUpdate()/UPDATE 未正确更新记录
我运行我的代码并正确完成设置。所有字符串确实都是字符串,所有整数都是整数。这里没有错误 - 程序执行更新时没有错误,并且输入值是应有的值。例如,我获取名为“First Last”的人的记录,并告诉代码将其更改为“Work Please”,但更新后,数据库显示它已更改为“-1 Last”。除了 FirstName 之外什么都没有改变 - 无论我输入什么,FirstName 都会更改为 -1。
相关片段:我已将其调试到执行它的位置,并且所有值都在那里 - 甚至是“Joe”。但相反,它决定更新整个记录,除了 FName 为“0”或“-1”外,不进行任何更改。
sql = "UPDATE Member SET FName = ? and LName = ? and Email = ? and "
+ "Year = ? and Shirt = ? and Comm = ? and Phone = ? and PPhone = ? and Events = ? where MemberID = ? ";
pstmt = dbConnection.prepareStatement(sql);
pstmt.setString(1, "Joe");
pstmt.setString(2, tempm.getName().split(" ")[1]);
pstmt.setString(3, tempm.getEmail());
pstmt.setInt(4, tempm.getYear());
pstmt.setInt(5, tempm.getShirt());
pstmt.setInt(6, tempm.getComm());
pstmt.setString(7, phonea[0]);
pstmt.setString(8, phonea[1]);
pstmt.setString(9, tempm.getEvents());
pstmt.setInt(10, tempm.getmID());
pstmt.executeUpdate();
从 和 更改为 ,给出
sql = "UPDATE Member SET FName = ?, LName = ?, Email = ?, "
+ "Year = ?, Shirt = ?, Comm = ?, Phone = ?, PPhone = ?, Events = ? where MemberID = ?";
Which 导致相同的结果。
I run my code with the setup done correctly. All the Strings are indeed strings, and all the ints are ints. There's no error here - the program executes the update without error and the input values are what they should be. For instance, I take the record for the person with the name "First Last", and tell the code to change it to "Work Please", but after updating, the database shows that it has been changed to "-1 Last". Nothing but the FirstName changes - and FirstName changes to -1 regardless of what I put in.
Relevant snippet: I've debugged it to the point where it executes it, and all the values are there - even "Joe". But instead, it decides to update the whole record with no changes except for FName being "0" or "-1".
sql = "UPDATE Member SET FName = ? and LName = ? and Email = ? and "
+ "Year = ? and Shirt = ? and Comm = ? and Phone = ? and PPhone = ? and Events = ? where MemberID = ? ";
pstmt = dbConnection.prepareStatement(sql);
pstmt.setString(1, "Joe");
pstmt.setString(2, tempm.getName().split(" ")[1]);
pstmt.setString(3, tempm.getEmail());
pstmt.setInt(4, tempm.getYear());
pstmt.setInt(5, tempm.getShirt());
pstmt.setInt(6, tempm.getComm());
pstmt.setString(7, phonea[0]);
pstmt.setString(8, phonea[1]);
pstmt.setString(9, tempm.getEvents());
pstmt.setInt(10, tempm.getmID());
pstmt.executeUpdate();
Changing from and to , gives
sql = "UPDATE Member SET FName = ?, LName = ?, Email = ?, "
+ "Year = ?, Shirt = ?, Comm = ?, Phone = ?, PPhone = ?, Events = ? where MemberID = ?";
Which leads to the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在更新语句中使用逗号而不是
and
:您当前的查询将被解释为:
括号中的部分是布尔表达式,其中
=
是相等比较运算符而不是您想要的赋值。You should use commas rather than
and
in your update statement:Your current query is being interpreted as:
The part in parentheses is a boolean expression, where the
=
is the equality comparison operator rather than assignment as you intended.也许尝试用“”包围你的答案,
我记得有一个类似的问题,一旦我这样做了,它就解决了,但没有任何承诺。
Maybe try surrounding your answers with ' '
I remember having a similar problem and once I did that it fixed it, no promises though.