Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package quiz_windows;
- import java.awt.Color;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.JPasswordField;
- import java.awt.Font;
- import java.awt.Window;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.sql.*;
- import javax.swing.*;
- public class Login {
- private JFrame LoginFrame;
- private JTextField textFieldUsN;
- private JPasswordField passwordField;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Login window = new Login();
- window.LoginFrame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- Connection connection = null;
- /**
- * Create the application.
- */
- public Login() {
- initialize();
- connection = sqliteConnection.dbConnector(); //Initialises connection
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- LoginFrame = new JFrame();
- LoginFrame.setBounds(100, 100, 691, 408);
- LoginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- LoginFrame.getContentPane().setLayout(null);
- JButton LoginBtn = new JButton("Login ");
- LoginBtn.setActionCommand("Login");
- LoginBtn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try {
- String query = "select * from StudentDetailsTbl where Username=? and Password=? "; //Makes a query to the database
- PreparedStatement pst = connection.prepareStatement(query); //Creates and object and passed the query to the PreparedStatement
- pst.setString(1, textFieldUsN.getText());
- pst.setString(2, passwordField.getText());
- ResultSet rs = pst.executeQuery();
- int count = 0;
- while (rs.next()) {
- count = count + 1;
- }
- if (count == 1) {
- JOptionPane.showMessageDialog(null, "Username and Password is correct");
- LoginFrame.dispose();
- new Mainmenu();
- } else {
- JOptionPane.showMessageDialog(null, "Username and Password is incorrect, Try again");
- }
- rs.close();
- pst.close(); //Closes connection to database
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, e);
- } finally {
- try {
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, e);
- }
- }
- }
- });
- LoginBtn.setBounds(312, 293, 89, 23);
- LoginBtn.setBackground(new Color(59, 89, 182));
- LoginBtn.setForeground(Color.WHITE);
- LoginFrame.getContentPane().add(LoginBtn);
- JLabel Usernamelbl = new JLabel("Username");
- Usernamelbl.setFont(new Font("Tahoma", Font.BOLD, 14));
- Usernamelbl.setBounds(237, 124, 89, 23);
- LoginFrame.getContentPane().add(Usernamelbl);
- JLabel Passwordlbl = new JLabel("Password");
- Passwordlbl.setFont(new Font("Tahoma", Font.BOLD, 14));
- Passwordlbl.setBounds(237, 179, 78, 17);
- LoginFrame.getContentPane().add(Passwordlbl);
- textFieldUsN = new JTextField();
- textFieldUsN.setBounds(347, 127, 97, 23);
- LoginFrame.getContentPane().add(textFieldUsN);
- textFieldUsN.setColumns(10);
- passwordField = new JPasswordField();
- passwordField.setBounds(347, 179, 97, 23);
- LoginFrame.getContentPane().add(passwordField);
- JButton btnRegister = new JButton("Register");
- btnRegister.setBackground(new Color(59, 89, 182));
- btnRegister.setForeground(Color.WHITE);
- btnRegister.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- LoginFrame.dispose();
- new Register();
- }
- });
- btnRegister.setBounds(576, 11, 89, 23);
- LoginFrame.getContentPane().add(btnRegister);
- LoginFrame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement