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

📄 loginjframe.java

📁 一个球员管理系统
💻 JAVA
字号:
/*
 * LoginJFrame.java
 */
package playermanager;
import java.sql.*;
import javax.swing.JOptionPane;



public class LoginJFrame extends javax.swing.JFrame {
    
    /** Creates new form LoginJFrame */
    public LoginJFrame() {
        initComponents();
    }
    public void validID() {
    
      String str1, str2;
      str1 = jTextField1.getText();
      str2 = jPasswordField1.getText();
      
     
          try {
     
      String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
      Driver driver = (Driver) Class.forName(driverName).newInstance();
      //连接的数据库是名为“data“的数据库
      //登录名是”sa“密码是“123”
      Connection con = DriverManager.getConnection(
          "jdbc:sqlserver://127.0.0.1:1433;", "sa", "123");
      con.setCatalog("data"); //数据库名称
      //执行动态的sql语句
      PreparedStatement pstmt = con.prepareStatement("select name,password from usernp where name=?");
      //将用户的名字添加到具体的sql语句中
      pstmt.setString(1, str1);
      //使用ResultSet中的方法executeQuery()来完成sql语句的执行
      ResultSet res = pstmt.executeQuery();
    
     
    if ((!res.next()) || res.getString("password") == null) {
        JOptionPane.showMessageDialog(this, "用户名错误", "错误提示",
                                      JOptionPane.ERROR_MESSAGE);
      }else {
                if ( !(res.getString("password").equals(str2))) 
                   {
                   JOptionPane.showMessageDialog(this, "密码错误", "错误提示",JOptionPane.ERROR_MESSAGE);
                   }
                      else{
                            loader();
                            this.setVisible(false);
                           }
             }
      
      pstmt.close();
      con.close();
    }catch (InstantiationException e) {
      System.out.println(e.getMessage());
    }catch (IllegalAccessException e) {
      System.out.println(e.getMessage());
    }catch (ClassNotFoundException e) {
      System.out.println(e.getMessage());
    }catch (SQLException edd) {
      edd.printStackTrace() ;
      System.out.println(edd.getMessage());
        }
    
  }
    
    
   public  void loader() {
   try {
       MainJFrame hh=new MainJFrame();
       hh.setVisible(true);
     }
   catch (Exception ew) {
      System.out.println(ew.getMessage());
       }
     }
   
   
    /** 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() {
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("\u7403\u5458\u7ba1\u7406\u7cfb\u7edf\u7ba1\u7406\u5458\u767b\u9646");
        setResizable(false);
        jLabel1.setText("\u7528\u6237\u540d \uff1a ");

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

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

        jButton2.setText("\u53d6\u6d88");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(jButton1)
                        .addGap(98, 98, 98)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))))
                .addGap(78, 78, 78))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// 登陆 
        validID();
       // this.setVisible(false);
       // MainJFrame hh=new MainJFrame();
       //hh.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
// 退出 
        System.exit(0);
    }//GEN-LAST:event_jButton2MouseClicked
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginJFrame().setVisible(true);
            }
        });
    }
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // 变量声明结束//GEN-END:variables
    
}

⌨️ 快捷键说明

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