传递数据参数以更新SQL表

时间:2013-05-06 11:12:26

标签: java sql swing

用户通过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 ( );

        }

    }

2 个答案:

答案 0 :(得分:3)

因为?周围放置了单引号。你不需要给出那些单引号,因为你这样做: -

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

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

答案 1 :(得分:0)

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

UPDATE systeuser SET username = ?, userpassword = ?, ...