简体   繁体   English

无法通过Java在Netbeans上执行SQL查询

[英]SQL Query not executing through Java on Netbeans

I'm trying to update my SQL database using JBDC. 我正在尝试使用JBDC更新我的SQL数据库。 But for some reason the query doesn't seem to be executing. 但是由于某种原因,查询似乎没有执行。 I'm using a Jframe GUI for my login screen and menu. 我将Jframe GUI用于登录屏幕和菜单。

What I'm trying to do is to get the account number entered in the login screen and take the amount entered by the user and minus that by the current balance and then pass the account number and the renaming amount to the database and update the account balance. 我想要做的是获取在登录屏幕中输入的帐号,然后将用户输入的金额减去当前余额后再减去,然后将该帐号和重命名金额传递给数据库并更新该帐户平衡。

This is the declaration part of some values: 这是一些值的声明部分:

public class MainMenu extends javax.swing.JFrame
{
    private Login log = new Login();

    private Account acc = null;
    private String accnum = log.getacctfld();

    Connection con = new DBConnection().connect();    
}

This is the code for the withdraw button: 这是撤回按钮的代码:

private void withdrawActionPerformed(java.awt.event.ActionEvent evt)                                         
{                                             
    String input = amountentered.getText();

    if ("".equals(input))
    {
        message.setText("Enter amount to perform operation");
        return;
    }

    Double amt = Double.parseDouble(input);

    if (amt > acc.getAmount() || amt <= 0.0) 
    {
        message.setText("INVALID AMOUNT. PLEASE TRY AGAIN.");

        return;
    }

    double bal = acc.getAmount() - amt;

    String command = "UPDATE user_atm SET balance =? WHERE accno =?";

    try
    {     
        PreparedStatement ps = con.prepareStatement(command);

        ps.setDouble(1, bal);
        ps.setString(2, accnum);

        ps.executeQuery(); // I think the query isn't executing
        ps.executeUpdate();

    }
    catch(Exception e)
    {

    }

    acc.setAmount(bal);

    balance.setText(bal + "");
    amountentered.setText("");

} 

UPDATE: 更新:

The accnum wasn't being initialized. accnum尚未初始化。 I was trying to pass the value from the JTextField from my Login screen. 我试图从登录屏幕传递JTextField的值。 But it wasn't working. 但这没有用。 So I passed the account number to the setter and then assigned accnum to the getter and now it's working. 因此,我将帐号传递给了setter,然后将accnum分配给了getter,现在可以使用了。

I also removed ps.executeQuery(); 我还删除了ps.executeQuery();

You must use executeUpdate() instead of executeQuery() 您必须使用executeUpdate()而不是executeQuery()
From what I understood you want to update data in the db isn't this case? 据我了解,您要更新数据库中的数据不是这种情况吗?
executeQuery() is used with SELECT statements to fetch rows and not udate. executeQuery()SELECT语句一起使用以获取行而不是udate。
So remove ps.executeQuery(); 因此删除ps.executeQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM