JAVA SQL如何在不擦除当前值的情况下更新列

时间:2018-05-12 21:09:45

标签: java oop user-interface methods

    public void actionPerformed(ActionEvent e) {
        String CardNumber = Card.getText();
        String CVV = LastThreeDigits.getText();
        String Exp = Expire.getText();
        String Amount = va.getText();
        String username = Username.getText();

        Customer c1 = new Customer(CardNumber, Exp, Amount, username);
        boolean validated = c1.VerifyCreditCard(CardNumber, CVV, Exp, Amount, username);

        if (validated) {
            try {
                String don = "Update Login set Funds= '" + Amount + "' where Username= '" + username + "' ";
                PreparedStatement man = connect.prepareStatement(don);

                man.executeUpdate();

                JOptionPane.showMessageDialog(null, "Updated"); `enter code here`
                AdminDashBoard dash = new AdminDashBoard();
                dash.show();
                frame.setVisible(false);

            } catch (Exception ex) {
                ex.printStackTrace();

            }
        } else if (!(validated)) {
            JOptionPane.showMessageDialog(null, "Missing fields");
        }
    }

我正在使用此方法更新包含列资金的表格。每当我输入值时,它会更新表格,但是如何在更新基金时将其添加到当前值?我对编码很新。

我的意思是,如果用户将值设置为10.它应该在数据库中显示10,如果用户想要说20,则数据库中的值应为30.

1 个答案:

答案 0 :(得分:0)

您可以将其附加到现有值:

Update Login set Funds = String.valueOf(Integer.parseInt(Funds) + Integer.parseInt('" + Amount + "')) where Username = '" + username + "' ";

或者更好的解决方案IMO是读取当前值,然后将它们一起添加到代码中,只需在现有set语句中设置新值。