Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import javax.swing.*;
- import javax.swing.border.EmptyBorder;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class Login extends JFrame {
- private JPanel p;
- private JLabel l;
- private JLabel l1;
- private JTextField t;
- private JTextField t1;
- private JButton loginB;
- private JButton registerB;
- private JButton quitB;
- private Connection conn;
- private Statement st;
- private ResultSet rs;
- public Login() {
- super("Login");
- setSize(483, 212);
- setLocationRelativeTo(null);
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- connect();
- buildPanel();
- add(p);
- setVisible(true);
- } // end of constructor
- private void connect() {
- try {
- final String DB_URL = "jdbc:odbc:FinalDB1";
- conn = DriverManager.getConnection(DB_URL);
- st = conn.createStatement();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- private void buildPanel() {
- p = new JPanel();
- p.setBorder(new EmptyBorder(5, 5, 5, 5));
- p.setLayout(null);
- l = new JLabel("Username");
- l.setFont(new Font("Tahoma", Font.PLAIN, 14));
- l.setBounds(72, 23, 78, 30);
- p.add(l);
- l1 = new JLabel("Password");
- l1.setFont(new Font("Tahoma", Font.PLAIN, 14));
- l1.setBounds(72, 68, 78, 30);
- p.add(l1);
- t = new JTextField(10);
- t.setBounds(198, 28, 137, 24);
- p.add(t);
- t1 = new JTextField(10);
- t1.setBounds(198, 73, 137, 24);
- p.add(t1);
- loginB = new JButton("Login");
- loginB.addActionListener(new LoginB());
- loginB.setBounds(54, 121, 89, 23);
- p.add(loginB);
- registerB = new JButton("Register");
- registerB.addActionListener(new RegisterB());
- registerB.setBounds(199, 121, 89, 23);
- p.add(registerB);
- quitB = new JButton("Quit");
- quitB.addActionListener(new QuitB());
- quitB.setBounds(337, 121, 89, 23);
- p.add(quitB);
- }// end of BuildPanel method
- private class LoginB implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- try {
- String user = t.getText().trim();
- String pass = t1.getText().trim();
- String sql = "SELECT Username, Password, AccountType FROM Accounts WHERE Username = '"
- + user + "' and Password = '" + pass + "' ";
- rs = st.executeQuery(sql);
- int count = 0;
- while (rs.next()) {
- count = count + 1;
- }
- if (count == 1) {
- sql = "SELECT * FROM Accounts WHERE Username = '" + user
- + "' ";
- rs = st.executeQuery(sql);
- String accountType = null;
- String fname = null;
- String lname = null;
- int id = 0;
- while (rs.next()) {
- accountType = rs.getString("AccountType");
- fname = rs.getString("FirstName");
- lname = rs.getString("LastName");
- id = rs.getInt("ID");
- }
- int accType = Integer.parseInt(accountType);
- if (accType == 0) {
- new StudentMenu(fname, lname, id); // opens the menu
- // for students
- conn.close();
- dispose();
- } else if (accType == 1) {
- new ProfessorMenu(fname, lname, id); // opens the menu
- // for professors
- conn.close();
- dispose();
- } else {
- JOptionPane.showMessageDialog(null,
- "Something went wrong!");
- }
- // JOptionPane.showMessageDialog(null, "Access Granted"); //
- // NO
- // NEED
- // MORE
- // THIS
- // AFTER
- // MENU
- // IS
- // CREATED
- } else if (count == 0) {
- JOptionPane.showMessageDialog(null,
- "Access Denied, Wrong Username and/or Password).");
- } else {
- JOptionPane.showMessageDialog(null, "Something went wrong");
- }
- //conn.close();
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- } // end of inner class
- private class RegisterB implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- try {
- conn.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- new Register();
- dispose();
- }
- } // end of inner class
- private class QuitB implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- try {
- conn.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- System.exit(0);
- }
- } // end of inner class
- } // end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement