Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. package gui;
  2.  
  3. import core.Employee;
  4.  
  5. import database.DBConnection;
  6. import gui.GeneralWindow;
  7. import java.awt.EventQueue;
  8. import javax.swing.*;
  9. import javax.swing.UIManager.LookAndFeelInfo;
  10. import java.awt.Font;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.ActionEvent;
  13. import java.sql.*;
  14.  
  15. public class AdminLogin {
  16.  
  17.     public static JFrame frame;
  18.     private static JTextField txtUsername;
  19.     private static JPasswordField txtPassword;
  20.     public static boolean isLoggedIn;
  21.     static Connection connection;
  22.  
  23.     /**
  24.      * Launch the application.
  25.      */
  26.     public static void main(String[] args) {
  27.        
  28.         try {
  29.             for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
  30.                 if ("Nimbus".equals(info.getName())) {
  31.                     UIManager.setLookAndFeel(info.getClassName());
  32.                     break;
  33.                 }
  34.             }
  35.         } catch (Exception e) {
  36.             // If Nimbus is not available, you can set the GUI to another look and feel.
  37.         }
  38.        
  39.         EventQueue.invokeLater(new Runnable() {
  40.             public void run() {
  41.                 try {
  42.                     AdminLogin window = new AdminLogin();
  43.                     window.frame.setVisible(true);
  44.                 } catch (Exception e) {
  45.                     e.printStackTrace();
  46.                 }
  47.             }
  48.         });
  49.     }
  50.  
  51.     /**
  52.      * Create the application.
  53.      */
  54.     public AdminLogin() {
  55.         initialize();
  56.     }
  57.  
  58.     /**
  59.      * Initialize the contents of the frame.
  60.      */
  61.     public static void initialize() {
  62.         frame = new JFrame();
  63.         frame.setBounds(100, 100, 850, 600);
  64.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  65.         frame.getContentPane().setLayout(null);
  66.         frame.setVisible(true);
  67.        
  68.         txtUsername = new JTextField();
  69.         txtUsername.setBounds(370, 165, 174, 41);
  70.         frame.getContentPane().add(txtUsername);
  71.         txtUsername.setColumns(10);
  72.        
  73.         txtPassword = new JPasswordField();
  74.         txtPassword.setBounds(370, 254, 174, 41);
  75.         frame.getContentPane().add(txtPassword);
  76.         txtPassword.setColumns(10);
  77.        
  78.         JButton btnNewButton = new JButton("Enter");
  79.         btnNewButton.setBounds(370, 350, 174, 31);
  80.         frame.getContentPane().add(btnNewButton);
  81.        
  82.         JLabel lblLoginAsAdministrator = new JLabel("Login as Administrator");
  83.         lblLoginAsAdministrator.setFont(new Font("Tahoma", Font.BOLD, 16));
  84.         lblLoginAsAdministrator.setHorizontalAlignment(SwingConstants.CENTER);
  85.         lblLoginAsAdministrator.setBounds(343, 51, 227, 79);
  86.         frame.getContentPane().add(lblLoginAsAdministrator);
  87.        
  88.         JLabel lblNewLabel = new JLabel("Username");
  89.         lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
  90.         lblNewLabel.setBounds(251, 165, 59, 41);
  91.         frame.getContentPane().add(lblNewLabel);
  92.        
  93.         JLabel lblNewLabel_1 = new JLabel("Password");
  94.         lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 11));
  95.         lblNewLabel_1.setBounds(251, 254, 59, 41);
  96.         frame.getContentPane().add(lblNewLabel_1);
  97.         frame.getRootPane().setDefaultButton(btnNewButton);
  98.  
  99.         btnNewButton.addActionListener(new ActionListener() {
  100.             @Override
  101.             public void actionPerformed(ActionEvent e) {
  102.                 try {
  103.                     connection = DBConnection.getConnection();
  104.  
  105.                     PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM employees where empID = ? and empPassword = ?");
  106.                     preparedStatement.setInt(1, Integer.valueOf(txtUsername.getText()));
  107.                     preparedStatement.setString(2, String.valueOf(txtPassword.getPassword()));
  108.                     ResultSet rs = preparedStatement.executeQuery();
  109.  
  110.                     if(rs.next()){
  111.                         isLoggedIn = true;
  112.                         frame.dispose();
  113.  
  114.  
  115.                     } else {
  116.                         isLoggedIn = false;
  117.                         PreparedStatement preparedStatement2 = connection.prepareStatement("SELECT * FROM admin where adminID = ? and adminPassword = ?");
  118.                         preparedStatement2.setInt(1, Integer.valueOf(txtUsername.getText()));
  119.                         preparedStatement2.setString(2, String.valueOf(txtPassword.getPassword()));
  120.                         ResultSet rs2 = preparedStatement2.executeQuery();
  121.  
  122.                         if(rs2.next()) {
  123.                             GeneralWindow window = new GeneralWindow();
  124.                             window.setVisible(true);
  125.                             frame.dispose();
  126.                             JOptionPane.showMessageDialog(null, "Welcome \nAdministrator: " + rs2.getString("fname"));
  127.                         }
  128.                         else{
  129.                             System.out.println("NOPE");
  130.                         }
  131.                     }
  132.  
  133.                 } catch(SQLException sqle){
  134.                     sqle.printStackTrace();
  135.                 }
  136.             }
  137.         });
  138.  
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement