Beispiel #1
0
  private void buttonLoginActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_buttonLoginActionPerformed
    boolean verify = false;
    String userName = usernameTextBox.getText();
    String passwd = passwordTextBox.getText();
    if (userName.equals("") && passwd.equals("")) {
      JOptionPane.showMessageDialog(
          AdminLogin.this, "Sorry! You must enter a Username and Password to login ");
      usernameTextBox.requestFocus();
    } else if (passwd.equals("")) {
      JOptionPane.showMessageDialog(AdminLogin.this, "Sorry! You must enter a Password to login ");
      usernameTextBox.requestFocus();
    } else if (userName.equals("")) {
      JOptionPane.showMessageDialog(AdminLogin.this, "Sorry! You must enter a Username to login ");
      usernameTextBox.requestFocus();
    } else {
      System.out.println("check: admin login success");
      Query query = null;
      //		EntityManagerFactory emf = Persistence.createEntityManagerFactory("smsPU");
      //		EntityManager em = emf.createEntityManager();

      query =
          School.manager
              .createQuery(
                  "select a from Administrator a where a.userName = :userName AND a.password = :password")
              .setParameter("userName", userName)
              .setParameter("password", passwd);
      System.out.println("Number of admins is/are : " + query.getResultList().size());
      verify = (query.getResultList().size() == 1) ? true : false;
      //		verify = (passwd.equals(admin.getPassword()) && userName.equals(admin.getUserName())) ?
      // true : false;
      if (verify) {

        Administrator admin = (Administrator) query.getSingleResult();
        this.dispose();
        AdminMainView adminView = new AdminMainView();
        adminView.btnEnabled();
        adminView.setVisible(true);
      } else {
        JOptionPane.showMessageDialog(
            AdminLogin.this, "Access Denied! Invalid Username or Password");
        usernameTextBox.setText("");
        passwordTextBox.setText("");
        usernameTextBox.requestFocus();
      }
      //            } catch (SQLException e) {
      //                System.out.println(e.getMessage());
      //            }

      // TODO add your handling code here:
    }
  } // GEN-LAST:event_buttonLoginActionPerformed
Beispiel #2
0
  private void usernameTextBoxKeyPressed(
      java.awt.event.KeyEvent evt) { // GEN-FIRST:event_usernameTextBoxKeyPressed
    // TODO add your handling code here:
    int key = evt.getKeyCode();
    if (key == KeyEvent.VK_ENTER) {
      boolean verify = false;
      String user = usernameTextBox.getText();
      String passwd = passwordTextBox.getText();

      if (passwd.equals("")) {
        JOptionPane.showMessageDialog(
            AdminLogin.this, "Sorry! You must enter a Password to login ");
        usernameTextBox.requestFocus();
      }

      if (user.equals("")) {
        JOptionPane.showMessageDialog(
            AdminLogin.this, "Sorry! You must enter a Username to login ");
        usernameTextBox.requestFocus();
      } else {
        Connection con = getConnection();
        try {
          ResultSet rows;
          Statement s =
              con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
          String select = "Select * from admin;";
          rows = s.executeQuery(select);
          while (rows.next()) {
            String username = rows.getString("Username");
            String password = rows.getString("Password");
            if (user.equals(username) && passwd.equals(password)) {
              verify = true;
              this.dispose();
              // JFrame frame1= new JFrame();
              AdminMainView adminBtns = new AdminMainView();
              adminBtns.btnEnabled();
              break;
            }
          }
          if (verify == false) {
            verify = false;
            usernameTextBox.setText("");
            passwordTextBox.setText("");
            usernameTextBox.requestFocus();
          }
        } catch (SQLException e) {
          System.out.println(e.getMessage());
        }
        if (verify == false) {
          JOptionPane.showMessageDialog(
              AdminLogin.this, "Access Denied!  Wrong Username or Password");
          usernameTextBox.setText("");
          passwordTextBox.setText("");
          usernameTextBox.requestFocus();
        }
      }
    }
    if (key == KeyEvent.VK_DOWN) {
      passwordTextBox.grabFocus();
    }
  } // GEN-LAST:event_usernameTextBoxKeyPressed