Java

Role based Login Application in Java Mysql

This Example will teach you how to do the Role based Login Application in Java MySQL. This application is very impotent when you do the industrial projects like hospital management system, inventory system, school management etc.

There are various actors if you take the example of hospital management system. have to give the different permission to each actors like Doctor,Patient,IT Enginner,Admin.

Login Form

Login form consist of three different roles those are Admin,Enginner,Receptionist. assign each actor role as different colors.color would be change based on the role.

Create there objects

Connection con;
PreparedStatement pst;
ResultSet rs;

Login button Code

        String username = txtUname.getText();
        String password = txtPass.getText();
        String utype = txtUtype.getSelectedItem().toString();

        try {
            Class.forName("com.mysql.jdbc.Driver");
             con = DriverManager.getConnection("jdbc:mysql://localhost/employeeinfo","root","");
            
            pst = con.prepareStatement("select * from user where uname =? and password =? and utype = ?");
            
            pst.setString(1, username);
            pst.setString(2, password);
             pst.setString(3, utype);
             
            rs= pst.executeQuery();
            
            if(rs.next())
            {
               int userid = rs.getInt("id");
               this.hide();
               new Main(username,utype).setVisible(true);
               
            }
            
            else
            {
                JOptionPane.showMessageDialog(this,"UserName and Password do not Match");
      
                
            }
          } catch (ClassNotFoundException ex) {
             ex.printStackTrace();
          
          } catch (SQLException ex) {
            
           ex.printStackTrace();
          
          }

 

 

Login Form Code

Main Form

if the user login successfully role will be shown to the Main form along with the color and username and user type.

Main Form Code

 String uname;
 String utype;
    
   public Main(String username, String utype) {
        
       
         initComponents();
          this.uname = username;
          
    
          this.utype = utype;
          lblUtype.setText(this.utype);
   
         
         String usertype =  lblUtype.getText().trim();
          
          if(utype.equals("admin"))
          {
          lblUser.setText(this.uname);
          lblUser.setForeground(Color.RED); 
                   
          lblUtype.setText(this.utype);         
           lblUtype.setForeground(Color.RED);          
                   
                   
          }
          else if(utype.equals("receptionist"))
          {
          lblUser.setText(this.uname);
          lblUser.setForeground(Color.BLUE); 
                   
          lblUtype.setText(this.utype);         
           lblUtype.setForeground(Color.BLUE);       
          }
          else
          {
          lblUser.setText(this.uname);
          lblUser.setForeground(Color.GREEN); 
                   
          lblUtype.setText(this.utype);         
           lblUtype.setForeground(Color.GREEN);       
          }
   
        
    }

i have attached the video link below. which will do this tutorials step by step.

 

 

admin

Recent Posts

Build Simple Water System Calculator in Java Using Swing

If you're just beginning to learn Java GUI programming creating an Water System Calculator is a fantastic project for…

4 days ago

GitHub Copilot vs Microsoft Copilot Best AI Tool to Use in 2025

GitHub is a powerful tool used by teams and developers around the globe. This guide is…

1 week ago

Chat with Claude AI Free – Your Super-Smart AI Buddy

It's like having a super-smart buddy that is always there to help you write stories,…

2 weeks ago

Best Festivals UK 2025 [Free Guide Included]

The UK is known for its rich history, diverse culture, and most of all  its…

2 weeks ago

Bank Holidays 2025 UK – Plan Your Perfect Long Weekends

Do you have a plan for your next holiday? Being aware of the Bank Holidays within the…

2 weeks ago

Master Cursor AI Full Guide for Students & Creators

The world is rapidly changing of software development AI-assisted tools for coding have become the main focus. As…

2 weeks ago