⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 login.java

📁 一个商品的进存销信息管理系统
💻 JAVA
字号:
//package jxc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;

public class Login
    extends JFrame {
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JLabel jLabel2 = new JLabel();
  JLabel jLabel3 = new JLabel();
  JTextField jTextField2 = new JTextField();
  JPasswordField jPasswordField1 = new JPasswordField();
  String user, password;
  int i;
  String config[] = {
      "", "", "", "", "", ""};
  Connection conn;
  Statement stmt;
  static String Loginuser, Loginpassword;
  static boolean Loginc, Logink, Loginx, Loginadministrator;

  public Login() {
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    jButton1.setBounds(new Rectangle(26, 107, 77, 27));
    jButton1.setText("确 定");
    jButton1.addActionListener(new Login_jButton1_actionAdapter(this));
    this.setLocale(java.util.Locale.getDefault());
    this.setResizable(false);
    this.setSize(new Dimension(230, 190));
    this.setTitle("登录");
    this.addWindowListener(new Login_this_windowAdapter(this));
    this.getContentPane().setLayout(null);
    jButton2.setBounds(new Rectangle(125, 107, 77, 27));
    jButton2.setText("取 消");
    jButton2.addActionListener(new Login_jButton2_actionAdapter(this));
    jLabel2.setText("用户");
    jLabel2.setBounds(new Rectangle(26, 24, 55, 22));
    jLabel3.setVerifyInputWhenFocusTarget(true);
    jLabel3.setText("密码");
    jLabel3.setBounds(new Rectangle(26, 63, 55, 22));
    jTextField2.setSelectionStart(11);
    jTextField2.setText("");
    jTextField2.addActionListener(new Login_jButton1_actionAdapter(this));
    jTextField2.setBounds(new Rectangle(79, 24, 132, 22));
    jPasswordField1.setVerifyInputWhenFocusTarget(true);
    jPasswordField1.addActionListener(new Login_jButton1_actionAdapter(this));
    jPasswordField1.setBounds(new Rectangle(79, 63, 132, 22));
    jPasswordField1.addMouseListener(new Login_jPasswordField1_mouseAdapter(this));
    this.getContentPane().add(jTextField2, null);
    this.getContentPane().add(jLabel2, null);
    this.getContentPane().add(jPasswordField1, null);
    this.getContentPane().add(jLabel3, null);
    this.getContentPane().add(jButton2, null);
    this.getContentPane().add(jButton1, 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++;
  //  }
  }

  void jButton2_actionPerformed(ActionEvent e) {
    System.exit(0);
  }

  private static boolean isPasswordCorrect(char[] input) {
    char[] correctPassword;
    correctPassword = Loginpassword.toCharArray(); //将数据库读取的密码放这里
    if (input.length != correctPassword.length) {
      return false;
    }
    for (int i = 0; i < input.length; i++) {
      if (input[i] != correctPassword[i]) {
		  System.out.print("密码错误!");
        return false;
      }
    }
    return true;
  }

  void jButton1_actionPerformed(ActionEvent e) {

       //取得用户输入
    String user = jTextField2.getText();
    char[] password = jPasswordField1.getPassword();
    //读取数据库里的用户密码权限
    try {
      //JDBC驱动
      try {
     //   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	  }
      catch (ClassNotFoundException ex) {
      }
      //config.dll内容:  config[0]为IP地址,config[1]为数据库的端口号:1433,
	  //config[2]为用户名,config[3]为密码,config[4]为数据库名称
    /*  conn = DriverManager.getConnection(
          "jdbc:microsoft:sqlserver://"
          + config[0] + ":"
          + config[1] + ";User="
          + config[2] + ";Password="
          + config[3] + ";DatabaseName="
          + config[4]);                     */
        conn=DriverManager.getConnection("jdbc:odbc:dic","","");

      stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery("select * from 用户 where 用户名 = 'Java'");
      //取值
      while (rs.next()) {
        Loginuser = rs.getString("用户名");
        Loginpassword = rs.getString("密码");
        Loginc = rs.getBoolean("采购权限");
        Logink = rs.getBoolean("库存权限");
        Loginx = rs.getBoolean("销售权限");
        Loginadministrator = rs.getBoolean("系统管理员");
      }
      stmt.close();
      conn.close();
    }
    catch (SQLException ex1) {
      System.out.print(ex1);
    }

     //登陆用户密码判断
    if (jTextField2.getText().toLowerCase().equals(Loginuser.toLowerCase()) &&
        isPasswordCorrect(password)) {
       //加载主窗体
      mainFrame frame = new mainFrame(user);
      //设置各种各种菜单的使用权限
    System.out.println();
      System.out.print(Loginadministrator);
      if (Loginadministrator == true) {
      }
      else {
        if (Loginc == false) {
          frame.jMenu2.setEnabled(false);
        }
        if (Logink == false) {
          frame.jMenu3.setEnabled(false);
        }
        if (Loginx == false) {
          frame.jMenu4.setEnabled(false);
        }
        frame.jMenu7.setEnabled(false);
      }
      //显示主界面
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = frame.getSize();
      if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
      }
      frame.setLocation( (screenSize.width - frameSize.width) / 2,
                        (screenSize.height - frameSize.height) / 2);
      frame.setVisible(true);
      hide();
    }
    else {
      JOptionPane.showMessageDialog(null, "登录失败",
                                    "出错",
                                    JOptionPane.ERROR_MESSAGE);
  
     }
  }

  void this_windowClosing(WindowEvent e) {
    System.exit(0);
  }

  void jPasswordField1_mouseClicked(MouseEvent e) {

  }

}

class Login_jButton2_actionAdapter
    implements java.awt.event.ActionListener {
  Login adaptee;

  Login_jButton2_actionAdapter(Login adaptee) {
    this.adaptee = adaptee;
  }

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

class Login_jButton1_actionAdapter
    implements java.awt.event.ActionListener {
  Login adaptee;

  Login_jButton1_actionAdapter(Login adaptee) {
    this.adaptee = adaptee;
  }

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

class Login_this_windowAdapter
    extends java.awt.event.WindowAdapter {
  Login adaptee;

  Login_this_windowAdapter(Login adaptee) {
    this.adaptee = adaptee;
  }

  public void windowClosing(WindowEvent e) {
    adaptee.this_windowClosing(e);
  }

}

class Login_jPasswordField1_mouseAdapter
    extends java.awt.event.MouseAdapter {
  Login adaptee;

  Login_jPasswordField1_mouseAdapter(Login adaptee) {
    this.adaptee = adaptee;
  }

  public void mouseClicked(MouseEvent e) {
    adaptee.jPasswordField1_mouseClicked(e);
  }
}

⌨️ 快捷键说明

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