2014-02-09 92 views
-4

此代碼與更改用戶密碼使用java從mysql table.this代碼是無錯誤的,但在此程序中有一個邏輯錯誤,它在sql語句中拋出異常,因爲我不不哪個查詢用於更新用戶密碼,請幫助我並通過我的電子郵件請求發送解決方案! [email protected]這是我的電子郵件ID。如何在java中使用jdbc更改用戶密碼

import java.awt.FlowLayout; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import java.sql.*; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 
import javax.swing.event.AncestorEvent; 
import javax.swing.event.AncestorListener; 

       public class ChangePassword extends JDialog implements ActionListener 
      { 
       JLabel userlbl,oldpasslbl,repasslbl,npasslbl,imglbl; 
       JTextField usernametxt; 
       JPasswordField oldpasstxt,repasstxt,npasstxt; 
       JButton changebtn; 

       public ChangePassword()throws ClassNotFoundException, SQLException, IOException 
       { 
        setVisible(true); 
        setTitle("Change Password"); 
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
        setBounds(300,300,600,300); 
        setLayout(null); 
        Font f = new Font("Microsoft New Tai Lue",Font.PLAIN,12); 
        Font f1 = new Font("Microsoft New Tai Lue",Font.PLAIN,14); 
        Font f2 = new Font("Microsoft New Tai Lue",Font.BOLD,18); 
        ImageIcon ICON = new ImageIcon("C:\\Users\\khan\\Desktop\\splash scren\\man.png"); 
        imglbl = new JLabel(ICON); 
        userlbl = new JLabel("Enter Username  :"); 
        userlbl.setFont(f); 
        oldpasslbl = new JLabel("Current Password  :"); 
        oldpasslbl.setFont(f); 
        npasslbl = new JLabel("New Password   :"); 
        npasslbl.setFont(f); 
        repasslbl = new JLabel("Re-Password   :"); 
        repasslbl.setFont(f); 

        usernametxt = new JTextField(); 
        usernametxt.setFont(f1); 
        oldpasstxt = new JPasswordField(); 
        oldpasstxt.setFont(f2); 
        npasstxt = new JPasswordField(); 
        npasstxt.setFont(f2); 
        repasstxt = new JPasswordField(); 
        repasstxt.setFont(f2); 

        changebtn = new JButton("Change"); 

        imglbl.setBounds(400, 30, 160, 130); 
        userlbl.setBounds(10, 30, 120, 20); 
        usernametxt.setBounds(180, 30, 200, 25); 
        oldpasslbl.setBounds(10, 60,120, 20); 
        oldpasstxt.setBounds(180, 60, 200, 25); 
        npasslbl.setBounds(10, 90, 120, 25); 
        npasstxt.setBounds(180, 90, 200, 25); 
        repasslbl.setBounds(10, 120, 120, 25); 
        repasstxt.setBounds(180, 120, 200, 25); 
        changebtn.setBounds(180, 160, 120, 30); 


        add(imglbl); 
        add(userlbl); 
        add(usernametxt); 
        add(oldpasslbl); 
        add(oldpasstxt); 
        add(npasslbl); 
        add(npasstxt); 
        add(repasslbl); 
        add(repasstxt); 
        add(changebtn); 

        changebtn.addActionListener(this); 
       } 

       public static void main(String args[]) 
       { 
        try 
        { 
         new ChangePassword(); 
        } 
        catch (ClassNotFoundException | SQLException | IOException e) 
        { 
         e.printStackTrace(); 
        } 
       } 

       public void actionPerformed(ActionEvent a) 
       { 
        if(a.getSource().equals(changebtn)) 
        { 
         int x = 0; 
         String cuser1 = usernametxt.getText(); 
         String old = oldpasstxt.getText(); 
         String npassword = npasstxt.getText(); 
         String repassword = repasstxt.getText(); 
         try 
         { 
          Connection con=null; 
          con = CreateConnection.connectMe(); 
          if(cuser1.equals("") && old.equals("") && npassword.equals("")) 
          { 
            JOptionPane.showMessageDialog(this, "PLEASE ENTER ALL INFORMATION"); 
          } 
          else 
          { 
           if(vali(cuser1,old)) 
           { 
            if(npassword.equals(old)) 
            { 
             JOptionPane.showMessageDialog(this, "PASSWORD IS ALL REDY EXIST PLEASE CHOOSE OTHER PASSWORD"); 
            } 
            else 
            { 
             if(npassword.equals(repassword)) 
             { 
              con = CreateConnection.connectMe(); 
              PreparedStatement st; 
              st = con.prepareStatement("UPDATE createaccount SET password= '+npassword+' where cuser = 'cuser1'"); 
      //        st.setString(1, cuser); 
              st.setString(1, npassword); 
              st.executeUpdate(); 
              JOptionPane.showMessageDialog(this, "PASSWORD UPDATE SUCCESSFUL"); 
             } 
             else 
             { 
              JOptionPane.showMessageDialog(this, "PASSWORD NOT MATCH"); 
              repasstxt.setText(null); 
             } 
            } 
           } 
           else 
           { 
            JOptionPane.showMessageDialog(this, "USERNAME NOT FOUND"); 
           } 
          } 
         } 
         catch(ClassNotFoundException e) 
         { 
          JOptionPane.showMessageDialog(this, "Connection class is not found"); 
         } 
         catch (SQLException e) 
         { 
          JOptionPane.showMessageDialog(this, "Query Not Executed"); 
         } 
         catch (IOException e) 
         { 
          JOptionPane.showMessageDialog(this, "Input/Output Error"); 
         } 
        }   
       } 
       public boolean vali(String cuser, String password) 
       { 
        boolean result = false; 
        Connection con; 
        try 
        { 
         con = CreateConnection.connectMe(); 
         PreparedStatement st; 
         st = con.prepareStatement("select * from createaccount where cuser = ? AND password = ?"); 
         st.setString(1, cuser); 
         st.setString(2, password); 
         ResultSet rs = st.executeQuery(); 
         if(rs.next()) 
         { 
          result = true; 
         } 
         else 
         { 
          result = false; 
         } 
        } 
        catch(ClassNotFoundException e) 
        { 
         JOptionPane.showMessageDialog(this, "Connection class is not found"); 
        } 
        catch (SQLException e) 
        { 
         JOptionPane.showMessageDialog(this, "Query Not Executed"); 
        } 
        catch (IOException e) 
        {![output of this code][1] 
         JOptionPane.showMessageDialog(this, "Input/Output Error"); 
        } 
        return result; 

      }                                  
+1

代碼看起來可怕,可考慮使用其他類連接到數據庫,並把所有的代碼,在那裏,它會更容易跟蹤誤差。此外,不同類別的GUI代碼不能混合使用同一類中的功能,請嘗試遵循單一責任 – Koitoer

+2

*「請幫助我,並通過我的電子郵件請求發送解決方案!」* ... **否** –

+1

更改DB密碼不推薦應用程序。 –

回答

1

您的代碼是正確的,錯誤在您的查詢中。試試這個:

if(ae.getSource().equals(deletebtn)) 
{ 
    int x = 0; 
    String cuser,answer,question1,password; 
    cuser = cusertxt.getText(); 
    password = passtxt.getText(); 
    answer = anstxt.getText(); 
    question1 = securitycb.getSelectedItem().toString(); 

    if(cusertxt.getText().isEmpty()) 
    { 
     JOptionPane.showMessageDialog(null, "Please Enter Username"); 
    } 
    else if(passtxt.getText().isEmpty()) 
    { 
     JOptionPane.showMessageDialog(null, "Please Enter Password"); 
    } 
    else if(securitycb.getSelectedItem().equals("---------------------------Questions----------------------------")) 
    { 
     JOptionPane.showMessageDialog(null, "Please Choose Security Questions"); 
    } 
    else if(anstxt.getText().isEmpty()) 
    { 
     JOptionPane.showMessageDialog(null, "Please enter your answer"); 
    } 
    else 
    { 
     int dialogButton = JOptionPane.YES_NO_OPTION; 
     int dialogResult = JOptionPane.showConfirmDialog(null, "Are you sure to delete your account", "Delete Account",dialogButton); 
     if(dialogResult == JOptionPane.YES_OPTION) 
     { 
      Connection con = null; 
      try 
      { 
       con = CreateConnection.connectMe(); 
       PreparedStatement st=null; 
       st = con.prepareStatement("delete from createaccount where cuser = ? AND password = ? AND question = ? AND ans = ?"); 
       st.setString(1, cuser); 
       st.setString(2, password); 
       st.setString(3, question1); 
       st.setString(4, answer); 
       int m = st.executeUpdate();                   
       if (m==1) 
       { 
        JOptionPane.showMessageDialog(null, " Successfully deleted"); 
        dispose(); 

        new LoginForm(); 
        //System.exit(0); 
       } 
       else 
       { 
        JOptionPane.showMessageDialog(null, "Sorry! This Username is dosen't exist "); 
       } 
      } 
      catch (ClassNotFoundException ce) 
      { 

      } 
      catch(SQLException s) 
      { 
       JOptionPane.showMessageDialog(null, "query not executed"); 
      } 
      catch(IOException e) 
      { 

      } 
     } 
     else 
     { 
      setVisible(false); 
     } 
    } 
} 
0

變化,並把這個代碼

if(a.getSource().equals(changebtn)) 
      { 
       int x = 0; 
       String cuser1 = usernametxt.getText(); 
       String old = oldpasstxt.getText(); 
       String npassword = npasstxt.getText(); 
       String repassword = repasstxt.getText(); 
       try 
       { 
        Connection con=null; 
        con = CreateConnection.connectMe(); 
        if(cuser1.equals("") || old.equals("") || npassword.equals("") || repassword.equals("")) 
        { 
          JOptionPane.showMessageDialog(null, "PLEASE ENTER ALL INFORMATION"); 
        } 
        else 
        { 
         if(vali(cuser1,old)) 
         { 
          if(npassword.equals(old)) 
          { 
           JOptionPane.showMessageDialog(null, "PASSWORD IS ALL REDY EXIST PLEASE CHOOSE OTHER PASSWORD"); 
          } 
          else 
          { 
           if(npassword.equals(repassword)) 
           { 
            con = CreateConnection.connectMe(); 
            PreparedStatement st; 
            st = con.prepareStatement("UPDATE createaccount SET password = ? where cuser = ?"); 
            st.setString(1, npassword); 
            st.setString(2, cuser1); 
            st.executeUpdate(); 
            JOptionPane.showMessageDialog(null, "PASSWORD UPDATE SUCCESSFUL"); 
            repasstxt.setText(null); 
            npasstxt.setText(null); 
            usernametxt.setText(null); 
            oldpasstxt.setText(null); 
            setVisible(false); 
           } 
           else 
           { 
            JOptionPane.showMessageDialog(null, "PASSWORD NOT MATCH"); 
            repasstxt.setText(null); 
            npasstxt.setText(null); 
           } 
          } 
         } 
         else 
         { 
          JOptionPane.showMessageDialog(null, "USERNAME NOT FOUND"); 
          repasstxt.setText(null); 
          npasstxt.setText(null); 
          usernametxt.setText(null); 
          oldpasstxt.setText(null); 
         } 
        } 
       } 
       catch(ClassNotFoundException e) 
       { 
        JOptionPane.showMessageDialog(null, "Connection class is not found"); 
       } 
       catch (SQLException e) 
       { 
        JOptionPane.showMessageDialog(null, "Query Not Executed"); 
       } 
       catch (IOException e) 
       { 
        JOptionPane.showMessageDialog(null, "Input/Output Error"); 
       } 
      }   


     } 

    };