Hi Friend,
Yes, you can. We are providing you the code for Change Password through which we have retrieved the id of particular password using select query and then using this id we have changed the existed password.
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChangePassword extends JFrame{
JLabel label1,label2,label3;
JPasswordField text1,text2,text3;
JButton button;
static JPanel panel;
String connectionURL = "jdbc:
mysql://localhost:3306/test";;;
Connection con;
public ChangePassword(){
label1=new JLabel("Old Password");
label2=new JLabel("New Password");
label3=new JLabel("Confirm Password");
text1=new JPasswordField(10);
text2=new JPasswordField(10);
text3=new JPasswordField(10);
button=new JButton("Change Password");
panel=new JPanel(new GridLayout(4,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(connectionURL, "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select id from login where password='"+value1+"'");
int id=0;
if(rs.next()){
id=rs.getInt("id");
}
else{
JOptionPane.showMessageDialog(null, "Invalid Password!", "Error", JOptionPane.ERROR_MESSAGE);
}
st.close();
if(value2.equals(value3)){
Statement st1=con.createStatement();
int i=st1.executeUpdate("update login set password='"+value2+"' where id='"+id+"'");
JOptionPane.showMessageDialog(null,"Your password is changed successfully.");
st1.close();
con.close();
}
else{
JOptionPane.showMessageDialog(null, "Invalid Confirm Password!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception exc){
System.out.println(exc);
}
}
});
}
public static void main(String[] args){
ChangePassword pass=new ChangePassword();
pass.setVisible(true);
pass.setSize(300,100);
pass.add(panel);
}
}
Thanks