📄 change_pwd.java
字号:
package user;
import javax.swing.*;
import java.sql.*;
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.*;
import javax.swing.JFrame;
public class change_pwd extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JPanel cp;
ImageIcon icon=new ImageIcon("picture/29.jpg");
JLabel tp=new JLabel(icon,JLabel.CENTER);
JLabel j1=new JLabel("账户名 : ");
JLabel j2=new JLabel("原始密码: ");
JLabel j3=new JLabel("新密码 :");
JLabel j4=new JLabel("确认密码:");
JButton b1=new JButton("修改");
JButton b2=new JButton("取 消");
TextField t1=new TextField(20);
TextField t2=new TextField(16);
TextField t3=new TextField(16);
TextField t4=new TextField(16);
public change_pwd()
{try{
Init();
}catch(Exception err){err.printStackTrace();}}
private void Init() throws Exception{
cp=(JPanel) this.getContentPane();
cp.setLayout(null);
this.setTitle("用户密码修改");
this.setResizable(false);
this.setSize(420,400);
j1.setBounds(80,40,100,30);
j1.setFont(new java.awt.Font("Dialog",2,20));
j1.setForeground(new Color(250,0,0));
j2.setBounds(80,90,100,30);
j2.setFont(new java.awt.Font("Dialog",2,20));
j2.setForeground(new Color(250,0,0));
j3.setBounds(80,140,100,30);
j3.setFont(new java.awt.Font("Dialog",2,20));
j3.setForeground(new Color(250,0,0));
j4.setBounds(80,190,100,30);
j4.setFont(new java.awt.Font("Dialog",2,20));
j4.setForeground(new Color(250,0,0));
t1.setBounds(200,40,120,30);
t1.setFont(new java.awt.Font("Dialog",0,15));
t1.addActionListener(this);
t2.setBounds(200,90,120,30);
t2.setFont(new java.awt.Font("Dialog",0,14));
t2.addActionListener(this);
t3.setBounds(200,140,120,30);
t3.setFont(new java.awt.Font("Dialog",0,14));
t3.addActionListener(this);
t4.setBounds(200,190,120,30);
t4.setFont(new java.awt.Font("Dialog",0,14));
t4.addActionListener(this);
b1.setBounds(80,280,90,30);
b1.setFont(new java.awt.Font("Dialog",0,15));
b1.addActionListener(this);
b2.setBounds(230,280,90,30);
b2.setFont(new java.awt.Font("Dialog",0,15));
b2.addActionListener(this);
tp.setText("");
tp.setSize(420,400);
cp.add(j1,null);
cp.add(j2,null);
cp.add(j3,null);
cp.add(j4,null);
cp.add(t1,null);
cp.add(t2,null);
cp.add(t3,null);
cp.add(t4,null);
cp.add(b1,null);
cp.add(b2,null);
cp.add(tp,null);
t2.setEchoChar('*');
t3.setEchoChar('*');
t4.setEchoChar('*');
this.setVisible(true);
this.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==b2){this.dispose();}
else if(t1.getText().equals("")){JOptionPane.showMessageDialog(null,"请输入用户名!");t1.requestFocus();t2.setText("");}
else if(e.getSource()==t1){t2.requestFocus();}
else if(t2.getText().equals("")){JOptionPane.showMessageDialog(null,"请输入原密码!");t2.requestFocus();}
else if(e.getSource()==t2){t3.requestFocus();}
else if(t3.getText().equals("")){JOptionPane.showMessageDialog(null,"请输入新密码!");t3.requestFocus();}
else if(e.getSource()==t3){t4.requestFocus();}
else if(t4.getText().equals("")){JOptionPane.showMessageDialog(null,"请输入确认密码!");t4.requestFocus();}
else if(e.getSource()==t4||e.getSource()==b1)
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:yundonghui","sa","123");
Statement ps=con.createStatement();
ResultSet rs=ps.executeQuery("select * from 用户管理 where 用户名='"+t1.getText().trim()+"'and 密码='"+t2.getText().trim()+"'");
if(!rs.next())
{
JOptionPane.showMessageDialog(null,"用户名或密码错误,请重新输入!");
t1.setText("");t2.setText("");t3.setText("");t4.setText("");t1.requestFocus();
}
else
{
if(t3.getText().equals(t4.getText()))
{
if(t3.getText().length()>=6&&t3.getText().length()<=16)
{
int v=JOptionPane.showConfirmDialog(this, "确认修改吗?","密码修改确认",JOptionPane.OK_CANCEL_OPTION);
if(v==JOptionPane.OK_OPTION)
{
ps.executeUpdate("Update 用户管理 set 密码='"+t3.getText().trim()+"'where 用户名='"+t1.getText().trim()+"'");
JOptionPane.showMessageDialog(null, "修改成功!");this.dispose();
}
else{this.dispose();}
}
else{JOptionPane.showMessageDialog(null, "密码必须是由6~16位之间的字符或数字组成!");t3.setText("");t4.setText("");t3.requestFocus();}
}
else{JOptionPane.showMessageDialog(null,"两次密码输入不一致,请重新输入!");t3.setText("");t4.setText("");t3.requestFocus();}
}
}
catch (Exception err) { err.printStackTrace();JOptionPane.showMessageDialog(null,"数据库连接出错!"); }
}
public static void main(String args[])
{new change_pwd(); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -