简体   繁体   中英

Change user password through JDBC

I have made the code to change the password but it's not working. This code is meant ot change the user passwod using Java from MySQL table.

private void btn_ModifierUserActionPerformed(java.awt.event.ActionEvent evt) {      
    try {
        String uname = txt_userName.getText();
        String upassword = txt_userPassword.getText();
        String newassword = txt_usernewrPassword.getText();
        String conNewpassword = txt_userconfirmerPassword.getText();
        String grade = jCom_userGrade.getSelectedItem().toString();
        byte[] img=null;`

        String sql = "SELECT * FROM users where ID='"+txt_userID.getText()+"'";
        pst=(PreparedStatement)conn.prepareStatement(sql);
        rs=pst.executeQuery();
        String passwd = rs.getString("password");
        if(rs.next()){
        if(!uname.equals("")){
            if ( upassword.equals(passwd)) {
             //JOptionPane.showMessageDialog(null, usrname);
                if (newassword.equals(conNewpassword)) {
                    if( !newassword.equals("") && !conNewpassword.equals("")){
                        String sqlup="UPDATE users SET userName='"+uname+"',password='"+newassword+"',Grade='"+grade+"',image='"+img+"' where ID'"+txt_userID.getText()+"'";
                        pst=(PreparedStatement) conn.prepareStatement(sqlup);
                        pst.execute();
                        JOptionPane.showMessageDialog(null, "mise à jour réussie");
                    }else{
                    JOptionPane.showMessageDialog(null, "S'IL VOUS PLAÎT ENTRER LES INFORMATIONS");
                    }

                } else {
                    JOptionPane.showMessageDialog(null, "S'IL VOUS PLAÎT CONFIRMER MOT DE PASSE");
            }
         }else{
            JOptionPane.showMessageDialog(null, "S'IL VOUS PLAÎT ENTRER LE MOT DE PASSE");
            }
        }else{
            JOptionPane.showMessageDialog(null, "S'IL VOUS PLAÎT ENTRER LE NOM D'UTILISATEUR");
            }

        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }

}                                          

Have a look here .

Move the cursor ( rs.next ) before doing this:

String passwd = rs.getString("password");

And please close your result sets!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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