-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;
}
代碼看起來可怕,可考慮使用其他類連接到數據庫,並把所有的代碼,在那裏,它會更容易跟蹤誤差。此外,不同類別的GUI代碼不能混合使用同一類中的功能,請嘗試遵循單一責任 – Koitoer
*「請幫助我,並通過我的電子郵件請求發送解決方案!」* ... **否** –
更改DB密碼不推薦應用程序。 –