我想先选择一个按钮进行更新以进行验证

时间:2019-05-05 17:25:27

标签: java mysql swing

我的问题是我有单个按钮,它将更新所有信息。但是在更新之前,我必须确保将要使用的密码正确无误,所以我需要先对其进行验证,然后如果正确无误,那么我将更新我表的数据。

我已经尝试将preparestatement与其他代码混合使用。我真的不知道我能否在尝试和追赶中投入很多尝试和追赶。请给我您的想法,以便我能够解决此问题。

我尝试了许多解决方案,但都无法解决,因为如果表外有try catch,表将不会更新。我要说的是先尝试再捕获,然后在内部选择下一步,再尝试再尝试更新表。

public void actionPerformed(ActionEvent e) {
            int num = JOptionPane.showConfirmDialog(null, "Do you want to Update? ", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (num == 0) {
                char x[] = accpanel_tft2.getPassword();
                char x2[] = accpanel_newpasstft.getPassword();
                char x3[] = accpanel_confirmpasstft.getPassword();
                String z = new String(x);
                String z2 = new String(x2);
                String z3 = new String(x3);
                try {
                    String clientid = accpanel_accnumID.getText();
                    String cardnum =  accpanel_cardID.getText();
                    String username = accpanel_tft.getText();
                    String datecreated = accpanel_dateholder.getText();
                    String  name = accpanel_tft3.getText();
                    String address = accpanel_tft4.getText();
                    String bday = datePicker3.getText();
                    String age = accpanel_tft6.getText();
                    String nationality = accpanel_tft7.getText();
                    String gender = accpanel_cbox1.getSelectedItem().toString();
                    String civilstatus = accpanel_cbox2.getSelectedItem().toString();
                    String spouse = accpanel_tft8.getText();
                    Connection conn = DriverManager.getConnection(dburl, userdb, passdb);
                    String squery = "select * from accounts where username = '" + accpanel_tft.getText() + "'";
                    Statement st = conn.createStatement();
                    ResultSet resultSet = st.executeQuery(squery);
                    while (resultSet.next()) {
                        String password = resultSet.getString("password");
                        String salt = resultSet.getString("salt");
                        boolean passwordMatch = PasswordUtils.verifyUserPassword(accpanel_tft.getText(), password, salt);
                        if (passwordMatch) {
                           try{
                               PreparedStatement preparedStatement = conn.prepareStatement("update accounts set accountnumber = ? , cardnumber = ? , username = ?, password = ?, salt = ?, datecreated = ?, name = ?, address = ?, birthday = ?, age = ?, nationality = ?, gender = ?, civilstatus = ?, spouse =  where username = ?" );
                               preparedStatement.setString(1,clientid);
                               preparedStatement.setString(2,cardnum);
                               preparedStatement.setString(3,username);
                               preparedStatement.setString(4,password);
                               preparedStatement.setString(5,salt);
                               preparedStatement.setString(6,datecreated);
                               preparedStatement.setString(7,name);
                               preparedStatement.setString(8,address);
                               preparedStatement.setString(9,bday);
                               preparedStatement.setString(10,age);
                               preparedStatement.setString(11,nationality);
                               preparedStatement.setString(12,gender);
                               preparedStatement.setString(13,civilstatus);
                               preparedStatement.setString(14,spouse);
                               preparedStatement.setString(15,username);
                               preparedStatement.executeUpdate();
                           }catch (Exception e1){
                               System.out.println(e1);
                           }

                        }else {
                            System.out.println("Failed");
                            System.out.println(password);
                            System.out.println(salt);
                        }
                    }

                    resultSet.close();
                    st.close();
                    conn.close();
                } catch (Exception e1) {
                    System.out.println(e1);
                }
            }

        }

我的预期结果是,如果密码正确,我的代码将首先验证密码,然后我将更新我的表。但如果没有,我将不会更新表格。

0 个答案:

没有答案