📄 changepassword.java
字号:
//package jxc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.io.BufferedReader;
import java.io.FileReader;
public class changepassword
extends JDialog {
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField jTextField1 = new JTextField();
static JPasswordField jPasswordField1 = new JPasswordField();
static JPasswordField jPasswordField2 = new JPasswordField();
static String user;
String password;
int i;
String config[] = {
"", "", "", "", "", ""};
Connection conn;
Statement stmt;
public changepassword(String currentuser) {
user = currentuser;
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
setModal(true);
this.setResizable(false);
this.setSize(new Dimension(300, 220));
this.setTitle("更改密码");
this.getContentPane().setLayout(null);
jButton2.setBounds(new Rectangle(55, 129, 77, 27));
jButton2.setText("确 定");
jButton2.addActionListener(new changepassword_jButton2_actionAdapter(this));
jButton3.setBounds(new Rectangle(165, 129, 77, 27));
jButton3.setText("取 消");
jButton3.addActionListener(new changepassword_jButton3_actionAdapter(this));
jLabel1.setText("用户名");
jLabel1.setBounds(new Rectangle(35, 25, 50, 22));
jLabel2.setText("旧密码");
jLabel2.setBounds(new Rectangle(35, 57, 50, 22));
jLabel3.setText("新密码");
jLabel3.setBounds(new Rectangle(35, 88, 50, 22));
jTextField1.setEnabled(true);
jTextField1.setEditable(false);
jTextField1.setText(user);
jTextField1.addActionListener(new changepassword_jButton2_actionAdapter(this));
jTextField1.setBounds(new Rectangle(118, 25, 150, 22));
jPasswordField1.setText("");
jPasswordField1.addActionListener(new changepassword_jButton2_actionAdapter(this));
jPasswordField1.setBounds(new Rectangle(118, 57, 150, 22));
jPasswordField2.setText("");
jPasswordField2.addActionListener(new changepassword_jButton2_actionAdapter(this));
jPasswordField2.setBounds(new Rectangle(118, 88, 150, 22));
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jLabel3, null);
this.getContentPane().add(jTextField1, null);
this.getContentPane().add(jPasswordField1, null);
this.getContentPane().add(jPasswordField2, null);
this.getContentPane().add(jButton3, null);
this.getContentPane().add(jButton2, null);
//读取SQL用户表,以验证用户密码
//读取配置文件
BufferedReader br = new BufferedReader(new FileReader("config.dll"));
while (br.ready()) {
config[i] = br.readLine().toString();
System.out.print("第" + i + "等于" + config[i]);
i++;
}
}
private static boolean isPasswordCorrect(char[] input) {
char[] correctPassword;
correctPassword = jPasswordField2.getPassword(); //将数据库读取的密码放这里
if (input.length != correctPassword.length) {
return false;
}
for (int i = 0; i < input.length; i++) {
if (input[i] != correctPassword[i]) {
return false;
}
}
return true;
}
void jButton3_actionPerformed(ActionEvent e) {
//退出
hide();
}
void jButton2_actionPerformed(ActionEvent e) {
//两个密码都不一样
if (!isPasswordCorrect(jPasswordField1.getPassword())) {
JOptionPane.showMessageDialog(null, "你输入的两次密码不相同!请从新设置密码!");
jPasswordField1.setText("");
jPasswordField2.setText("");
return;
}
//密码长度小于6
if (jPasswordField1.getPassword().length < 6) {
JOptionPane.showMessageDialog(null, "你输入的密码小于6位");
jPasswordField1.setText("");
jPasswordField2.setText("");
return;
}
//更改密码
try {
//JDBC驱动
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch (ClassNotFoundException ex) {
}
//地址
conn = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://"
+ config[0] + ":"
+ config[1] + ";User="
+ config[2] + ";Password="
+ config[3] + ";DatabaseName="
+ config[4]);
stmt = conn.createStatement();
stmt.execute("update 用户 set 密码='" +
new String(jPasswordField1.getPassword()) +
"' where 用户名='" + user + "'");
stmt.close();
conn.close();
}
catch (SQLException ex1) {
System.out.print(ex1);
}
//退出
hide();
}
}
class changepassword_jButton3_actionAdapter
implements java.awt.event.ActionListener {
changepassword adaptee;
changepassword_jButton3_actionAdapter(changepassword adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
class changepassword_jButton2_actionAdapter
implements java.awt.event.ActionListener {
changepassword adaptee;
changepassword_jButton2_actionAdapter(changepassword adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -