Java Application using Swing to log in using sql database

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.AbstractButton;

public class AdminFrame2 extends JFrame implements ActionListener, WindowListener, MouseListener {

    private OkButton Ok = new OkButton("Log In");
    private MyButton Ca = new MyButton("Cancel");
    private JTextField tf;
    private JTextField pf;

    private Container cp;
    private JLabel label = new JLabel("Enter USERNAME: ");
    private JLabel label1 = new JLabel("Enter PASSWORD: ");
   
    public AdminFrame2() {
        super("Administrator GUI");
        setBounds(200, 200, 400, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tf = new JTextField(10);
        pf = new JTextField(10);
        FlowLayout flow = new FlowLayout();
        setLayout(flow);
        cp = getContentPane();

        cp.add(label);
        cp.add(tf);
        cp.add(label1);
        cp.add(pf);
        cp.add(Ok);
        cp.add(Ca);

        tf.setHorizontalAlignment(SwingConstants.CENTER);
        pf.setHorizontalAlignment(SwingConstants.CENTER);



        Ok.addActionListener(this);
        Ca.addActionListener(this);
        setVisible(true);
    }

        String data = "jdbc:odbc:VideoStore";

    public void windowActivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
    public void windowClosing(WindowEvent e) {}

    public void mouseClicked(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}

    class MyButton extends JButton {
        MouseListener m = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                System.exit(0);
            }
        };
             
        public MyButton(String label){
            super(label);
                    addMouseListener(m);
            }
   
    }

    class OkButton extends JButton {
        MouseListener m = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
               
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection(data, "", "");
            Statement st = conn.createStatement();
            ResultSet rec = st.executeQuery("SELECT users_name, users_password FROM USERS");

        while (rec.next()) {
            String x = tf.getText();
            String y = pf.getText();
           

            if (x == rec.getString("users_name")) {
                if (y == rec.getString("users_password")) {
                    System.out.println("Logged in!");
                } else {
                   new WinFocus(); //new next window
                    setVisible(false);
                }
            } else {
                     JOptionPane.showMessageDialog(null,"Invalid username or password");
            }

        }

        st.close();
        } catch(SQLException d) {
            System.out.println(e.toString());
        } catch(ClassNotFoundException f) {
            System.out.println(e.toString());
        }
        }

        };
             
        public OkButton(String label){
            super(label);
                    addMouseListener(m);
            }
   
    }



    public void actionPerformed(ActionEvent e) {
    }

    public static void main(String[] args) {
        new AdminFrame2();
    }

    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}  
Reactions

Post a Comment

0 Comments