2

用户通过 8 个 JTextField 输入他的信息。此信息通过参数传递给 updateUser 方法,该方法使用此信息更新 SQL 表。这是我目前正在尝试做的事情,尽管当用户按下更新按钮时,数据库表没有任何更改

检索用户数据,创建 UserOperation 并调用 updateUser 方法

   String userName = textFieldUserName.getText();
                String userPassword = textField_1.getText();
                String securityQuestion = textFieldSecQuestion.getText();
                String securityAnswer = textFieldAnswer.getText();
                String email = textFieldEmail.getText();
                String fName = textFieldName.getText();
                String phoneNumber = textFieldPhone.getText();
                String department = textFieldDepartment.getText();

                UserOperations userOperations = new UserOperations();
                userOperations.updateUser(userName, userPassword, securityQuestion, securityAnswer, email, fName, phoneNumber, department);

updateUser 方法

     public void updateUser (String userName, String userPassword, String securityQuestion, String securityAnswer, String email, String fName, String phoneNumber, String department)
    {

        try
        {
            String sql = "UPDATE systeuser SET username = ?,userpassword = ?,securityquestion = ?,securityanswer = ?,email = ?,fname = ?,phonenumber = ?,department = ? WHERE username = ?";
              stmt = conn.prepareStatement(sql);
              stmt.setString (1, userName);
              stmt.setString (2, userPassword);
              stmt.setString (3, securityQuestion);
              stmt.setString (4, securityAnswer);
              stmt.setString (5, email);
              stmt.setString (6, fName);
              stmt.setString (7, phoneNumber);
              stmt.setString (8, department);
              stmt.setString (9, userName);
              stmt.executeUpdate( );
        }
        catch (Exception e)
        {
            e.printStackTrace ( );

        }

    }
4

2 回答 2

4

这是因为在?. 您无需提供这些单引号,因为您正在这样做:-

stmt.setString(1, userName); // setString says that this parameter is a String

String因此,在您的情况下,不需要通常用于包含在查询中的单引号。

于 2013-05-06T11:17:03.397 回答
0

不要将用于查询参数的问号放在引号中。相反,使用:

UPDATE systeuser SET username = ?, userpassword = ?, ...
于 2013-05-06T11:20:42.910 回答