Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. package quiz_windows;
  2.  
  3. import java.awt.Color;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JButton;
  8. import javax.swing.JLabel;
  9. import javax.swing.JTextField;
  10. import javax.swing.JPasswordField;
  11. import java.awt.Font;
  12. import java.awt.Window;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.ActionEvent;
  15.  
  16.  
  17. import java.sql.*;
  18. import javax.swing.*;
  19.  
  20. public class Login {
  21.  
  22.  private JFrame LoginFrame;
  23.  private JTextField textFieldUsN;
  24.  private JPasswordField passwordField;
  25.  
  26.  /**
  27.   * Launch the application.
  28.   */
  29.  public static void main(String[] args) {
  30.   EventQueue.invokeLater(new Runnable() {
  31.    public void run() {
  32.     try {
  33.      Login window = new Login();
  34.      window.LoginFrame.setVisible(true);
  35.     } catch (Exception e) {
  36.      e.printStackTrace();
  37.     }
  38.    }
  39.   });
  40.  }
  41.  
  42.  Connection connection = null;
  43.  
  44.  /**
  45.   * Create the application.
  46.   */
  47.  public Login() {
  48.   initialize();
  49.   connection = sqliteConnection.dbConnector(); //Initialises connection
  50.  }
  51.  
  52.  /**
  53.   * Initialize the contents of the frame.
  54.   */
  55.  private void initialize() {
  56.   LoginFrame = new JFrame();
  57.   LoginFrame.setBounds(100, 100, 691, 408);
  58.   LoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59.   LoginFrame.getContentPane().setLayout(null);
  60.  
  61.   JButton LoginBtn = new JButton("Login ");
  62.   LoginBtn.setActionCommand("Login");
  63.   LoginBtn.addActionListener(new ActionListener() {
  64.    public void actionPerformed(ActionEvent arg0) {
  65.     try {
  66.      String query = "select * from StudentDetailsTbl where Username=? and Password=? "; //Makes a query to the database
  67.      PreparedStatement pst = connection.prepareStatement(query); //Creates and object and passed the query to the PreparedStatement
  68.      pst.setString(1, textFieldUsN.getText());
  69.      pst.setString(2, passwordField.getText());
  70.  
  71.      ResultSet rs = pst.executeQuery();
  72.      int count = 0;
  73.      while (rs.next()) {
  74.       count = count + 1;
  75.  
  76.      }
  77.      if (count == 1) {
  78.       JOptionPane.showMessageDialog(null, "Username and Password is correct");
  79.       LoginFrame.dispose();
  80.       new Mainmenu();
  81.      } else {
  82.       JOptionPane.showMessageDialog(null, "Username and Password is incorrect, Try again");
  83.      }
  84.      rs.close();
  85.      pst.close(); //Closes connection to database
  86.     } catch (Exception e) {
  87.      JOptionPane.showMessageDialog(null, e);
  88.     } finally {
  89.      try {
  90.  
  91.      } catch (Exception e) {
  92.       JOptionPane.showMessageDialog(null, e);
  93.      }
  94.     }
  95.  
  96.    }
  97.  
  98.   });
  99.  
  100.   LoginBtn.setBounds(312, 293, 89, 23);
  101.   LoginBtn.setBackground(new Color(59, 89, 182));
  102.   LoginBtn.setForeground(Color.WHITE);
  103.   LoginFrame.getContentPane().add(LoginBtn);
  104.  
  105.   JLabel Usernamelbl = new JLabel("Username");
  106.   Usernamelbl.setFont(new Font("Tahoma", Font.BOLD, 14));
  107.   Usernamelbl.setBounds(237, 124, 89, 23);
  108.   LoginFrame.getContentPane().add(Usernamelbl);
  109.  
  110.   JLabel Passwordlbl = new JLabel("Password");
  111.   Passwordlbl.setFont(new Font("Tahoma", Font.BOLD, 14));
  112.   Passwordlbl.setBounds(237, 179, 78, 17);
  113.   LoginFrame.getContentPane().add(Passwordlbl);
  114.  
  115.   textFieldUsN = new JTextField();
  116.   textFieldUsN.setBounds(347, 127, 97, 23);
  117.   LoginFrame.getContentPane().add(textFieldUsN);
  118.   textFieldUsN.setColumns(10);
  119.  
  120.   passwordField = new JPasswordField();
  121.   passwordField.setBounds(347, 179, 97, 23);
  122.   LoginFrame.getContentPane().add(passwordField);
  123.  
  124.   JButton btnRegister = new JButton("Register");
  125.   btnRegister.setBackground(new Color(59, 89, 182));
  126.   btnRegister.setForeground(Color.WHITE);
  127.   btnRegister.addActionListener(new ActionListener() {
  128.    public void actionPerformed(ActionEvent e) {
  129.     LoginFrame.dispose();
  130.     new Register();
  131.    }
  132.  
  133.   });
  134.   btnRegister.setBounds(576, 11, 89, 23);
  135.   LoginFrame.getContentPane().add(btnRegister);
  136.  
  137.   LoginFrame.setVisible(true);
  138.  }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement