开发者社区 问答 正文

如何使我的sql更新方法起作用?

我正在执行一种方法,该方法应该更新我在本地数据库中具有的标签(卡)的位置,该方法(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

展开
收起
保持可爱mmm 2019-11-29 19:43:26 360 分享 版权
阿里云 AI 助理回答

你的问题可能出在SQL语句的字符串拼接上,以及你如何处理方法返回值以确保只有在更新成功时才输出成功消息。以下是对你的代码进行的一些修正和建议:

  1. SQL语句字符串拼接错误:在构建SQL查询时,你没有正确地闭合单引号。这可能导致SQL语法错误或者SQL注入安全风险。使用PreparedStatement可以解决这两个问题。

  2. 检查更新影响的行数:你应该检查executeUpdate()方法返回的值,这个值表示受影响的行数。如果为0,则说明没有行被更新,即记录不存在或更新条件不匹配。

  3. 逻辑调整:只有当更新操作成功(即至少一行受到影响)时,才打印成功消息并返回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();
        }
    }
}

请确保在钱包类中也有一个接受所有字段的构造函数,尽管从你的描述来看,它似乎已经存在但未完全展示。此外,记得在实际应用中处理好异常,比如通过日志系统记录详细的错误信息,而不是简单地打印到控制台。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答分类:
SQL
问答标签:
问答地址: