password_modify.java~18~

来自「使用Java语言开发的数据库课程设计——医药管理信息系统。」· JAVA~18~ 代码 · 共 247 行

JAVA~18~
247
字号
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Password_modify extends JFrame
{
    XYLayout xYLayout1 = new XYLayout();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JTextField jTextField1 = new JTextField();
    JPasswordField jPasswordField1 = new JPasswordField();
    JPasswordField jPasswordField2 = new JPasswordField();
    JPasswordField jPasswordField3 = new JPasswordField();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();

    String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    String conURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CASY";
    String Username = "sa";
    String Password = "111111";


    public Password_modify()
    {
        this.setSize(400,330);
        this.setResizable(false);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        setLocation((screenSize.width - frameSize.width) / 2,
                    (screenSize.height - frameSize.height) / 2);

        try
        {
            jbInit();
        } catch (Exception exception)
        {
            exception.printStackTrace();
        }
        this.setVisible(true);
    }

    private void jbInit() throws Exception
    {
        getContentPane().setLayout(xYLayout1);
        jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 14));
        jLabel1.setText("用户ID:");
        jLabel4.setFont(new java.awt.Font("宋体", Font.BOLD, 14));
        jLabel4.setText("重复新密码:");
        jLabel3.setFont(new java.awt.Font("宋体", Font.BOLD, 14));
        jLabel3.setText("新密码:");
        jLabel2.setFont(new java.awt.Font("宋体", Font.BOLD, 14));
        jLabel2.setText("原密码:");
        jButton2.setText("重新输入");
        jButton2.addActionListener(new Password_modify_jButton2_actionAdapter(this));
        jButton1.setText("保存修改");
        jButton1.addActionListener(new Password_modify_jButton1_actionAdapter(this));
        xYLayout1.setWidth(400);
        xYLayout1.setHeight(300);
        jTextField1.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                jTextField1_keyTyped(e);
            }
        });
        jPasswordField1.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                jPasswordField1_keyTyped(e);
            }
        });
        jPasswordField2.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                jPasswordField2_keyTyped(e);
            }
        });
        jPasswordField3.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                jPasswordField3_keyTyped(e);
            }
        });
        this.getContentPane().add(jLabel1, new XYConstraints(67, 36, -1, -1));
        this.getContentPane().add(jLabel2, new XYConstraints(68, 84, -1, -1));
        this.getContentPane().add(jLabel3, new XYConstraints(68, 131, -1, -1));
        this.getContentPane().add(jLabel4, new XYConstraints(38, 179, -1, -1));
        this.getContentPane().add(jTextField1,
                                  new XYConstraints(147, 32, 152, 25));
        this.getContentPane().add(jPasswordField1,
                                  new XYConstraints(147, 80, 152, 25));
        this.getContentPane().add(jPasswordField2,
                                  new XYConstraints(147, 127, 152, 25));
        this.getContentPane().add(jPasswordField3,
                                  new XYConstraints(147, 175, 152, 25));
        this.getContentPane().add(jButton2, new XYConstraints(230, 230, 87, 41));
        this.getContentPane().add(jButton1, new XYConstraints(98, 230, 88, 41));
        this.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                new mainWin();
                dispose();
            }
        });

        this.validate();
    }

    public void Save()
    {
        if(jTextField1.getText().equals("")||jPasswordField1.getPassword().length==0
           ||jPasswordField2.getPassword().length==0||jPasswordField3.getPassword().length==0)
        {
            JOptionPane.showMessageDialog(null," 还有数据没填写,请重新输入 !!!","系统提示",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            new Load_DB_Driver();
            try
            {
                Connection con = DriverManager.getConnection(conURL,Username,Password);
                Statement s= con.createStatement();
                ResultSet rs = s.executeQuery("SELECT * FROM identify WHERE ID ='"+jTextField1.getText()+"' AND Password ='"+ String.valueOf(jPasswordField1.getPassword())+"';");
               if(rs.next()==false)
                {
                    JOptionPane.showMessageDialog(null,"用户"+jTextField1.getText()+"不存在或密码错误!","Error",JOptionPane.WARNING_MESSAGE);
                    jPasswordField1.setText("");
                    jPasswordField2.setText("");
                    jPasswordField3.setText("");
                }
                else
                {
                    if(!(String.valueOf(jPasswordField2.getPassword()).equals(String.valueOf(jPasswordField3.getPassword()))))
                    {
                        JOptionPane.showMessageDialog(null," 两次输入的新密码不相同,请重新输入 !"," 有错误 ",JOptionPane.WARNING_MESSAGE);
                        jPasswordField2.setText("");
                        jPasswordField3.setText("");
                    }
                    else
                    {
                        s.execute("update identify set password='"+String.valueOf(jPasswordField2.getPassword())+"' where ID='"+jTextField1.getText()+"'");
                        JOptionPane.showMessageDialog(null, "☆ 操作成功 !! ☆", "系统消息",
                                                  JOptionPane.WARNING_MESSAGE);
                        jPasswordField1.setText("");
                        jPasswordField2.setText("");
                        jPasswordField3.setText("");
                    }
                }
                s.close();
                con.close();

            }
            catch(SQLException ex)
            {
                JOptionPane.showMessageDialog(null,"SQLException:" + ex.getMessage(),"警告!",JOptionPane.WARNING_MESSAGE);
            }
        }

    }

    public static void main(String[] args)
    {
        new Password_modify();
    }

    public void jButton2_actionPerformed(ActionEvent e)
    {
        jTextField1.setText("");
        jPasswordField1.setText("");
        jPasswordField2.setText("");
        jPasswordField3.setText("");
         jTextField1.requestFocus();
    }

    public void jButton1_actionPerformed(ActionEvent e)
    {
        Save();
    }

    public void jTextField1_keyTyped(KeyEvent e)
    {
        if (e.getKeyChar()=='\n')
        Save();
    }

    public void jPasswordField1_keyTyped(KeyEvent e)
    {
        if (e.getKeyChar()=='\n')
        Save();
    }

    public void jPasswordField2_keyTyped(KeyEvent e)
    {
        if (e.getKeyChar()=='\n')
        Save();
    }

    public void jPasswordField3_keyTyped(KeyEvent e)
    {
        if (e.getKeyChar()=='\n')
        Save();
    }
}



class Password_modify_jButton1_actionAdapter implements ActionListener {
    private Password_modify adaptee;
    Password_modify_jButton1_actionAdapter(Password_modify adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}


class Password_modify_jButton2_actionAdapter implements ActionListener {
    private Password_modify adaptee;
    Password_modify_jButton2_actionAdapter(Password_modify adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?