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

📄 userlogin.java

📁 资产管理系统的数据库设计 (一)开发背景 随着我国高等教育事业的迅猛发展
💻 JAVA
字号:
/*
 * UserLogin.java
 *
 * Created on 2007年12月2日, 下午3:18
 */

package libAdmin;
import java.sql.*;
/**
 *
 * @author  Yu
 */
public class UserLogin extends javax.swing.JFrame {
    /** Creates new form UserLogin */
    public UserLogin() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        userIDField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jPasswordField = new javax.swing.JPasswordField();
        notify = new javax.swing.JLabel();

        jTextField2.setText("jTextField2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("\u6b22\u8fce\u8fdb\u5165\u7f51\u4e8c\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf");
        setResizable(false);
        jLabel1.setText("\u7528\u6237\u540d\uff1a");

        jLabel2.setText("\u5bc6  \u7801\uff1a");

        jButton1.setText("\u767b\u5f55");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("宋体", 0, 18));
        jLabel3.setText("\u6b22\u8fce\u8fdb\u5165\u7f51\u4e8c\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf");

        notify.setFont(new java.awt.Font("宋体", 0, 18));
        notify.setText("Wellcome!");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(115, 115, 115)
                .add(jButton1)
                .addContainerGap(117, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(96, 96, 96)
                .add(notify, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 133, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(60, Short.MAX_VALUE))
            .add(layout.createSequentialGroup()
                .add(29, 29, 29)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel3)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPasswordField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                            .add(userIDField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE))
                        .add(41, 41, 41))))
        );

        layout.linkSize(new java.awt.Component[] {jPasswordField, userIDField}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 30, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(userIDField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(22, 22, 22)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton1)
                .add(16, 16, 16)
                .add(notify, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(45, 45, 45))
        );

        layout.linkSize(new java.awt.Component[] {jPasswordField, userIDField}, org.jdesktop.layout.GroupLayout.VERTICAL);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO 连接数据库,验证用户名和密码
        String JDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String conURL = "jdbc:odbc:pejuwe";
        try {
            Class.forName(JDriver);
        } catch (ClassNotFoundException ex) {
            System.out.println("forname:" + ex.getMessage());
        }
        try{
            Connection con = DriverManager.getConnection(conURL);
            Statement s = con.createStatement();
            
            String userID = userIDField.getText().toString();
            String userPassword = new String(jPasswordField.getPassword());//这里不能用toString,不然是乱码。
            String validate = "select UserID,Password,IsAdmin from UserPsd where UserID = '" + userID+"'";
            System.out.println(validate);
            ResultSet rs = s.executeQuery(validate);
            if(rs.next()){
                String correctPsd = rs.getString("Password");
                if(!userPassword.equals(correctPsd)){
                    notify.setText("密码错误");
                } else{
                    //notify.setText("登录成功");
                    boolean isAdmin = rs.getBoolean("IsAdmin");
                    if(isAdmin){
                        AdminInterface adminInterface = new AdminInterface();
                        int screanWidth = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width);
                        int screanHeigth = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
                        adminInterface.setLocation((screanWidth-adminInterface.getSize().width)/2,(screanHeigth-adminInterface.getSize().height)/2);
                        adminInterface.setVisible(true);
                        adminInterface.setTitle("欢迎 " + userID + " 来到图书馆管理系统");
                        this.dispose();
                    } else{
                        UserInterface userInterface = new UserInterface(userID);
                        int screanWidth = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width);
                        int screanHeigth = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
                        userInterface.setLocation((screanWidth-userInterface.getSize().width)/2,(screanHeigth-userInterface.getSize().height)/2);
                        userInterface.setVisible(true);
                        userInterface.setTitle("欢迎 " + userID +" 来到网二图书馆");
                        this.dispose();
                    }
                }
            } else
                notify.setText("用户名错误");
            s.close();
            con.close();
            
        }catch(SQLException e){
            System.out.println("SQLException: " + e.getMessage());
            notify.setText("服务器链接错误");
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new UserLogin().setVisible(true);
            }
        });
    }
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JLabel notify;
    private javax.swing.JTextField userIDField;
    // 变量声明结束//GEN-END:variables
    
}

⌨️ 快捷键说明

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