我正在执行一种方法,该方法应该更新我在本地数据库中具有的标签(卡)的位置,该方法(UpdateWallet)接收用户的输入,并根据用户插入的内容覆盖数据库中的记录。在此方法代码的末尾,如果在数据库中找到了输入的“标签”并成功更新了其位置,则它应返回true并输出一条消息,说明更新已成功。否则,返回false但是我有2个问题:
1-虽然我收到前一条消息(它已成功更新),但是值没有更新。
2-无论我输入什么值,我的方法都满意,并声称它“更新”了记录。即使插入的标签在我的数据库中不存在!在这种情况下,它应该只返回false。
我的更新方法:
public boolean UpdateWallet(wallet upWallet) throws SQLException { Connection dbConnection = null; Statement statement = null;
String update = " UPDATE wallets SET Location = '"+upWallet.getWalletLocation()+"' WHERE Tag = '"
+ upWallet.getWalletTag()+"';";
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
// execute SQL update
statement.executeUpdate(update);
System.out.println("It has been updated successfully");
} catch (SQLException e) {
System.out.println(e.getMessage());
return false;
} finally {
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
return true;
} 控制器代码:
System.out.println("Please enter a tag to update its location"); String tag = input.next();
System.out.println("Please enter Location "); String loc = input.next();
wallet w = new wallet(tag,loc); WalletsDAO.UpdateWallet(w); 钱包类:
public class wallet { private String Name; private String Location; private String Tag;
public wallet(String Name, String Location, String Tag) {
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
}
public wallet(String Tag) {
this.Tag = Tag;
}
public wallet(String Location, String Tag) {
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
}
public String getWalletName() {
return Name;
}
public void setWalletName(String Name) {
this.Name = Name;
}
public String getWalletLocation() {
return Location;
}
public void setWalletLocation(String Location) {
this.Location = Location;
}
public String getWalletTag() {
return Tag;
}
public void setWalletTag(String Tag) {
this.Tag = Tag;
}
@Override
public String toString() {
String p = (this.Name + ',' + this.Location + ',' + this.Tag);
return p;
}
} 有人可以帮我吗?
问题来源于stack overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
你的问题可能出在SQL语句的字符串拼接上,以及你如何处理方法返回值以确保只有在更新成功时才输出成功消息。以下是对你的代码进行的一些修正和建议:
SQL语句字符串拼接错误:在构建SQL查询时,你没有正确地闭合单引号。这可能导致SQL语法错误或者SQL注入安全风险。使用PreparedStatement
可以解决这两个问题。
检查更新影响的行数:你应该检查executeUpdate()
方法返回的值,这个值表示受影响的行数。如果为0,则说明没有行被更新,即记录不存在或更新条件不匹配。
逻辑调整:只有当更新操作成功(即至少一行受到影响)时,才打印成功消息并返回true。
下面是修改后的UpdateWallet
方法示例:
public boolean UpdateWallet(wallet upWallet) throws SQLException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String updateQuery = "UPDATE wallets SET Location = ? WHERE Tag = ?";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(updateQuery);
// 设置参数,防止SQL注入
preparedStatement.setString(1, upWallet.getWalletLocation());
preparedStatement.setString(2, upWallet.getWalletTag());
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows > 0) {
System.out.println("It has been updated successfully");
return true;
} else {
System.out.println("No records were updated. Check the Tag provided.");
return false;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
return false;
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
请确保在钱包类中也有一个接受所有字段的构造函数,尽管从你的描述来看,它似乎已经存在但未完全展示。此外,记得在实际应用中处理好异常,比如通过日志系统记录详细的错误信息,而不是简单地打印到控制台。