login.java~128~

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

JAVA~128~
241
字号
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.Cursor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JRootPane;
import java.awt.Image;

public class Login extends JFrame
{
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JPasswordField pwf1 = new JPasswordField();
    JTextField jTextField1 = new JTextField();
    JButton jButton1 = new JButton();
    Icon bg = new ImageIcon(".\\images\\login.jpg");
    Icon button = new ImageIcon(".\\images\\login_button.jpg");
    String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
    String conURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CASY";
    String Username = "sa";
    String Password = "111111";
    String lack = "select 药品编号,药品名称 from Medicine_Data where 库存量<=50";
    JPanel jPanel1 = new JPanel();
    XYLayout xYLayout2 = new XYLayout();
    JLabel jLabel4 = new JLabel();
    JPanel jPanel2 = new JPanel();
    XYLayout xYLayout1 = new XYLayout();

    public Login()
    {
        this.setSize(600, 319);
        this.setResizable(false);
        this.setTitle("系统登录");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(".\\images\\jxc.ICO")));
        Toolkit kit=Toolkit.getDefaultToolkit();
        Image image = kit.createImage(".\\images\\id.jpg");
        setIconImage(image);

        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);
        jLabel2.setFont(new java.awt.Font("新宋体", Font.BOLD, 12));
        jLabel2.setBorder(null);
        jLabel2.setActionMap(null);
        jLabel2.setDisplayedMnemonic('0');
        jLabel2.setText("用户 ID:");
        jLabel3.setFont(new java.awt.Font("新宋体", Font.BOLD, 12));
        jLabel3.setText("用户口令:");
        jButton1.addActionListener(new Login_jButton1_actionAdapter(this));
        pwf1.setFont(new java.awt.Font("Dialog", Font.BOLD, 24));
        pwf1.setNextFocusableComponent(jButton1);
        pwf1.setEchoChar('★');
        pwf1.addKeyListener(new Login_pwf1_keyAdapter(this));
        jPanel1.setLayout(xYLayout2);
        jLabel4.setFont(new java.awt.Font("宋体", Font.PLAIN, 14));
        jLabel4.setIcon(bg);
        jButton1.setIcon(button);
        jButton1.setCursor(new Cursor(Cursor.HAND_CURSOR));
        xYLayout1.setWidth(600);
        xYLayout1.setHeight(319);
        jPanel1.setOpaque(false);
        jTextField1.setFont(new java.awt.Font("宋体", Font.BOLD, 24));
        jTextField1.setNextFocusableComponent(pwf1);
        jTextField1.addKeyListener(new Login_jTextField1_keyAdapter(this));
        jPanel2.add(jLabel4);
        jPanel1.add(jLabel2, new XYConstraints(58, 133, 66, 31));
        jPanel1.add(jTextField1, new XYConstraints(145, 134, 225, 29));
        jPanel1.add(jLabel3, new XYConstraints(58, 188, 67, 31));
        jPanel1.add(pwf1, new XYConstraints(144, 189, 225, 29));
        jPanel1.add(jButton1, new XYConstraints(422, 135, 89, 91));
        this.getContentPane().add(jPanel1, new XYConstraints(0, 0, 600, 319));
        this.getContentPane().add(jPanel2, new XYConstraints(0, 0, 600, 319));
        this.validate();
    }

    public void verify()
    {
        if(jTextField1.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null,"输入用户ID不能为空,请重新输入!!!","系统提示",JOptionPane.ERROR_MESSAGE);
            jTextField1.requestFocus();
        }
        else if (pwf1.getPassword().length<=0)
        {
            JOptionPane.showMessageDialog(null,"输入用户口令不能为空,请重新输入!!!","系统提示",JOptionPane.ERROR_MESSAGE);
            pwf1.requestFocus();
        }
       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(pwf1.getPassword())+"';");
               if(rs.next()==false)
                {
                    JOptionPane.showMessageDialog(null,"用户"+jTextField1.getText()+"不存在或密码错误!","Error",JOptionPane.WARNING_MESSAGE);
                    pwf1.setText("");
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"成功登录!","您好!",JOptionPane.WARNING_MESSAGE);
                    dispose();
                    try
                    {
                        Connection con2 = DriverManager.getConnection(conURL,
                                Username,
                                Password);
                        Statement s2 = con.createStatement();
                        ResultSet rs2 = s2.executeQuery(lack);
                        if (rs2.next())
                            JOptionPane.showMessageDialog(null,
                                    "有药品的库存量不足50,请到每日提示查看!!",
                                    "警告!",
                                    JOptionPane.WARNING_MESSAGE);
                        rs.close();
                        con.close();
                    }
                   catch(SQLException ex)
                   {
                         JOptionPane.showMessageDialog(null,"SQLException:" + ex.getMessage(),"警告!",JOptionPane.WARNING_MESSAGE);
                   }
                   new mainWin();
                }
                s.close();
                con.close();
            }
            catch(SQLException ex)
            {
                JOptionPane.showMessageDialog(null,"SQLException:" + ex.getMessage(),"警告!",JOptionPane.WARNING_MESSAGE);
            }
        }

    }

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

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

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

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


class Login_jTextField1_keyAdapter extends KeyAdapter {
    private Login adaptee;
    Login_jTextField1_keyAdapter(Login adaptee) {
        this.adaptee = adaptee;
    }

    public void keyTyped(KeyEvent e) {
        adaptee.jTextField1_keyTyped(e);
    }
}

class Login_pwf1_keyAdapter extends KeyAdapter {
    private Login adaptee;
    Login_pwf1_keyAdapter(Login adaptee) {
        this.adaptee = adaptee;
    }

    public void keyTyped(KeyEvent e) {
        adaptee.pwf1_keyTyped(e);
    }
}

class Login_jButton1_actionAdapter implements ActionListener
{
    private Login adaptee;
    Login_jButton1_actionAdapter(Login adaptee) {
        this.adaptee = adaptee;
    }

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

⌨️ 快捷键说明

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