如何使用jdbc更改java中的用户密码

时间:2014-02-09 07:42:41

标签: java

这段代码与使用mysql table中的java更改用户密码相关。这段代码没有错误但是在这个程序中有一个逻辑错误,它在sql语句中抛出异常因为我不知道哪个查询用于更新用户密码请帮助我,并在我的电子邮件请求发送解决方案! khanusama111@gmail.com这是我的电子邮件ID。

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;

            }                                                                                                                                       

2 个答案:

答案 0 :(得分: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);
        }
    }
}

答案 1 :(得分: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");
                }
            }           


        }

    };