JAVA Program to Display an Login Form

JAVA Program to Display an Login Form:

//User Name :-om, password bhushan;
import java.awt.*;
import  java.awt.event.*;
import javax.swing.*;
import java.sql.*;


public class login extends JFrame implements ActionListener
{
    JLabel  lb1,j1,lb2,lb3,lb4,lb5,lb6;
    JLabel image;
    JButton b1,b2,b3,b4,b5,b6;
    JTextField tf1, tf3, tf6;

    JPasswordField tf2;
    JPasswordField tf4,tf5;

    Container cp;
    Connection con;

    login()
    {
        super("Login Screen");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container cp=getContentPane();
        cp.setLayout(null);
        cp.setBackground(Color.white);
        
                                   

        j1=new JLabel("LOGIN SCREEN",JLabel.CENTER);
        j1.setFont(new Font("Times New Roman",Font.BOLD,37));
        j1.setBorder(BorderFactory.createMatteBorder(0,0,3,0,Color.red));
        j1.setBounds(0,0,1050,50);
        cp.add(j1);

        lb1=new JLabel("Login Name");
        lb1.setFont(new Font("Times New Roman",Font.BOLD,20));
        lb1.setBounds(120,120,250,25);
        cp.add(lb1);

        tf1=new JTextField(40);
        tf1.setFont(new Font("Times New Roman",Font.BOLD,20));
        tf1.setBounds(250,120,120,25);
        cp.add(tf1);
        
        lb2=new JLabel("Password");
        lb2.setFont(new Font("Times New Roman",Font.BOLD,20));
        lb2.setBounds(120,200,150,25);
        cp.add(lb2);

        tf2=new JPasswordField(40);
        tf2.setFont(new Font("Times New Roman",Font.BOLD,20));
        tf2.setBounds(250,200,120,25);
        cp.add(tf2);
        
        lb3=new JLabel("New Login");
        lb3.setFont(new Font("Times New Roman",Font.BOLD,20));
        lb3.setBounds(400,350,150,25);
        lb3.setVisible(false);
        cp.add(lb3);

        tf3=new JTextField(40);
        tf3.setFont(new Font("Times New Roman",Font.BOLD,20));
        tf3.setBounds(550,350,210,25);
        tf3.setVisible(false);
        cp.add(tf3);
        
        lb4=new JLabel("New Password");
        lb4.setFont(new Font("Times New Roman",Font.BOLD,20));
        lb4.setBounds(400,410,150,25);
        lb4.setVisible(false);
        cp.add(lb4);
        
        tf4=new JPasswordField(40);
        tf4.setFont(new Font("Times New Roman",Font.BOLD,20));
        tf4.setBounds(550,410,210,25);
        tf4.setVisible(false);
        cp.add(tf4);

        lb6=new JLabel("Old Login");
        lb6.setFont(new Font("Times New Roman",Font.BOLD,20));
        lb6.setBounds(400,520,150,25);
        lb6.setVisible(false);
        cp.add(lb6);

        tf6=new JTextField(40);
        tf6.setFont(new Font("Times New Roman",Font.BOLD,20));
        tf6.setBounds(550,520,210,25);
        tf6.setVisible(false);
        cp.add(tf6);

        lb5=new JLabel("Old Password");
        lb5.setFont(new Font("Times New Roman",Font.BOLD,20));
        lb5.setBounds(400,460,150,25);
        lb5.setVisible(false);
        cp.add(lb5);

        tf5=new JPasswordField(40);
        tf5.setFont(new Font("Times New Roman",Font.BOLD,20));
        tf5.setBounds(550,460,210,25);
        tf5.setVisible(false);
        cp.add(tf5);

        b1=new JButton("LOGIN");
        b1.setFont(new Font("Times New Roman",Font.BOLD,17));
        b1.setBounds(100,300,100,35);
        b1.setToolTipText("Confirm password");
        b1.setMnemonic('L');
        b1.addActionListener(this);
        cp.add(b1);

        /*b2=new JButton("CANCEL");
        b2.setFont(new Font("Times New Roman",Font.BOLD,17));
        b2.setBounds(250,300,120,35);
        b2.setToolTipText("Exit from password");
        b2.setMnemonic('C');
        b2.addActionListener(this);
        cp.add(b2);*/
        
        b3=new JButton("CLEAR");
        b3.setFont(new Font("Times New Roman",Font.BOLD,17));
        b3.setBounds(250,300,120,35);
        b3.setToolTipText(" Clear the Textfields");
        b3.setMnemonic('C');
        b3.addActionListener(this);
        cp.add(b3);

        b4=new JButton("CHANGE PASSWORD");
        b4.setFont(new Font("Times New Roman",Font.BOLD,17));
        b4.setBounds(400,300,210,35);
        b4.setToolTipText("Change the password");
        b4.setMnemonic('P');
        b4.addActionListener(this);
        cp.add(b4);
        
        image=new JLabel(new ImageIcon("key1.gif"));
        image.setBounds(400,80,200,200);
        cp.add(image);
        
        setSize(1400,1400);
        setVisible(true);
        setExtendedState(MAXIMIZED_BOTH);
        
        connection();
    }
    void connection()
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("Driver");
            con=DriverManager.getConnection("jdbc:odbc:inventry");
            System.out.println("Connection");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
    public void actionPerformed(ActionEvent  ae)
    {
        if(ae.getSource()==b1)
        {
            try
            {
                String query="select * from login where loginname=\'"+tf1.getText()+"\' and pass=\'"+tf2.getText()+"\'";
                Statement stat=con.createStatement();
                ResultSet rs=stat.executeQuery(query);

                if(rs.next())
                {
                //  new option();
                    dispose();
                }
                else
                {
                JOptionPane.showMessageDialog(this,"Access Denied");                System.out.println(query);
                }
            }
            catch(Exception e){}
        }//if
        /*if(ae.getSource()==b2)
        {
            System.exit(0);
        }*/
        if(ae.getSource()==b3)
        {
            tf1.setText("");
            tf2.setText("");
        }
        if(ae.getSource()==b4)
        {
            if(b4.getText().equals("CHANGE PASSWORD"))
            {
                tf3.setVisible(true);
                tf4.setVisible(true);
                tf5.setVisible(true);
                tf6.setVisible(true);
                
                lb3.setVisible(true);
                lb4.setVisible(true);
                lb5.setVisible(true);
                lb6.setVisible(true);

                b4.setText("OK");
                b4.setMnemonic('O');
            }
            else
            {
                try
                {
                    String login=tf3.getText();
                    String new1=tf4.getText();
                    String old=tf5.getText();
                    String oldlog=tf6.getText();
String query="Update login set loginname=?, pass=? where loginname=? and pass=?";

            PreparedStatement pstat=con.prepareStatement(query);
                    pstat.setString(1,login);
                    pstat.setString(2,new1);
                    pstat.setString(3,oldlog);
                    pstat.setString(4,old);
                    int no=pstat.executeUpdate();
                    if(no>0)
                    {
            JOptionPane.showMessageDialog(this,"Password Changed");
                    }
                    else
            JOptionPane.showMessageDialog(this,"Access Denied");

                    
                    tf3.setText("");
                    tf4.setText("");
                    tf5.setText("");
                    tf6.setText("");

                    tf3.setVisible(false);
                    tf4.setVisible(false);
                    tf5.setVisible(false);
                    tf6.setVisible(false);

                    lb3.setVisible(false);
                    lb4.setVisible(false);
                    lb5.setVisible(false);
                    lb6.setVisible(false);

                    b4.setText("CHANGE PASSWORD");

                }
                catch(Exception e)
                {
                    System.out.println(e);
                }
            }
        }
    }//actionperformed
    public static void main(String args[])
    {
        login obj=new login();
    }   
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.