Wednesday, 6 November 2019

Swing Login

Hi All,
Use this code
-----------------------------------------------
package placement;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
//import javax.swing.JOptionPane;
import javax.swing.*;


class Login extends Frame implements ActionListener{
TextField tid;
String userid;
String driveid;
final JPasswordField tpass;
Button b1,b2;
   
Login(String userid,String password)
{
System.out.println(userid+" "+password);
       
setTitle("Login");
setVisible(true);   

setLayout(new FlowLayout());
this.setLayout(null);
Label uid=new Label("UserId:",Label.CENTER);
Label pass=new Label("Password:",Label.CENTER);



  tid=new TextField(20);
  tpass=new JPasswordField(20);
 


      uid.setBounds(70,90,90,60);
      pass.setBounds(70,130,90,60);
     


        tid.setBounds(200,100,90,20);
       tpass.setBounds(200,140,90,20);
     
    b1=new Button("Close");
        b1.setBounds(200,200,70,40);
b2=new Button("Login");
    b2.setBounds(280,200,70,40);
 

    b1.addActionListener(this);
b2.addActionListener(this);


add(tid);
add(tpass);



add(b1);
add(b2);

this.add(uid);
this.add(pass);



setSize(500,500);
setLayout(null);
tid.setText(userid);
tpass.setText(password);


setVisible(true);

}

 Login(){
     setBackground(Color.getHSBColor(97,92,247));
    setTitle("Login");
setVisible(true);   
      setLayout(new FlowLayout());
this.setLayout(null);
Label useid=new Label("UserId:",Label.CENTER);
Label password=new Label("password:",Label.CENTER);



  tid=new TextField(20);
  tpass=new JPasswordField(20);
 



      useid.setBounds(70,90,90,60);
      password.setBounds(70,130,90,60);
     


        tid.setBounds(200,100,90,20);
        tpass.setBounds(200,140,90,20);
       

    b1=new Button("Close");
        b1.setBounds(200,200,70,40);
b2=new Button("Login");
    b2.setBounds(280,200,70,40);
   

    b1.addActionListener(this);
b2.addActionListener(this);


add(tid);
add(tpass);



add(b1);
add(b2);


this.add(useid);
this.add(password);




setSize(400,350);
setLayout(null);
setVisible(true);
                               
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
              if(str.equals("Close"))
{
                    int confirm = JOptionPane.showOptionDialog(
             null, "Are You Sure to Close Application?",
             "Exit Confirmation", JOptionPane.YES_NO_OPTION,
             JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == 0) {
            new Homepage();
           setVisible(false);
       
    }

                    setVisible(false);
}
if(str.equals("Login"))
{
String useid=tid.getText();
                                        String drive=tid.getText();
String password=new String(tpass.getPassword());
                                         userid=useid;
                                         driveid=drive;
                                         setVisible(false);


try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/placement","root","");
   //here sonoo is database name, root is username and password
    Statement stmt=con.createStatement();
                                    String qr="select * from login where id='"+userid+"'and password='"+password+"'and status='1'";
                                    System.out.println(qr);
    ResultSet rs=stmt.executeQuery(qr);
  int flag=0;
                                   while(rs.next())
                                   {
                                       flag=1;
                                       String type=rs.getString("type");
                                       if(type.equals("po"))
                                       {
                                         POmenu pomenu=new POmenu(driveid);
                                         pomenu.setVisible(true);
                                       }
                                       else if(type.equals("student"))
                                       {
                                         StudMenu studmenu=new StudMenu(userid);
                                         studmenu.setVisible(true);
                                       }
                                       else if(type.equals("company"))
                                       {
                                         ITMenu itmenu=new ITMenu();
                                         itmenu.setVisible(true);
                                       }
                                        else if(type.equals("Admin"))
                                       {
                                         AdminMenu adminmenu=new AdminMenu();
                                         adminmenu.setVisible(true);
                                       }
                                   }
                                  if(flag==0)
                                 
                                      JOptionPane.showMessageDialog(null,"Invalid Username or Password,Try Again");
                                 
    con.close();
}
catch(Exception e1)
{
System.out.println(" Error :"+e1);
}
}




/*if(str.equals("update"))
{
String id=tid.getText();
String password=tpass.getText();
String type=ttype.getText();
String statuts=tstat.getText();

try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/placement","root","");
   //here sonoo is database name, root is username and password
    Statement stmt=con.createStatement();
    stmt.executeUpdate("update fund set password='"+tf2.getText()+"',type='"+tf3.getText()+"',amt='"+tf4.getText()+"',date='"+tf5.getText()+"',actcredited='"+tf6.getText()+"' where id='"+tf1.getText()+"'");
   // ResultSet rs=stmt.executeQuery("select * from tbl_job");
   // while(rs.next())
   // System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));
    con.close();
}
catch(Exception e1)
{
System.out.println(e1);
}
}*/
}
public static void main(String args[])
{
new Login();
}
}






No comments:

Post a Comment

Swing Menu

Hi All, Use this code. ------------------------------------------------------- package placement; import java.awt.*; import java.io.*...