Multi level User Login in Java Application

Java is becoming very popular due to its reliability and ability to make an application. Moreover with Java, development of an application made easier . Included is create a login system which is an essential element of an application . This tutorial explains how to create a login system using Java and make multilevel login from java Net-beans IDE.

Variables to initialize first.

package import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 *
 * @author MSANGI
 */
public class Login extends javax.swing.JFrame {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;  


public Login() {
     initComponents();
    }
 

Mouse evevent Listerner For the login Button

private void login_btnMouseClicked(java.awt.event.MouseEvent evt) {                                       
    if(usr.getText().trim().equals("") && pwd.getText().trim().equals("")){
            error.setForeground(Color.red);
            error.setText("<html><b>user</b> and <b>password</b> fields are empty<html>");
    }else if(usr.getText().trim().equals("")){
            error.setForeground(Color.red);
            error.setText("<html><b>username</b> is required<html>");
    }else if(pwd.getText().trim().equals("")){
            error.setForeground(Color.red);
            error.setText("<html><b>password</b> is required<html>");
    }else
        try {
            con = Konekt.java_db();
            String sql="SELECT * FROM users WHERE username =? and password= ? ";
            pst = con.prepareStatement(sql);
            pst.setString(1, usr.getText());
            pst.setString(2, pwd.getText().trim());
            rs = pst.executeQuery();
            if(rs.next()){
                String lvl = rs.getString("userlevel");
                String status = rs.getString("ac_status");
                jina =rs.getString("fullname");
                user_ID = rs.getString("userID");
                if(lvl.equals("User")&& status.equals("0") || lvl.equals("Admin")&& status.equals("0")
                    {
                    JOptionPane.showMessageDialog(null,"Account is Suspended","null",JOptionPane.ERROR_MESSAGE);
                    System.exit(0);
                }
                if(lvl.equals("User")){
                    User user= new User();
                    nurse.setVisible(true); this.dispose();login_activity();
                }if(lvl.equals("Admin")){
                    Admin admin = new Admin();
                    admin.setVisible(true); this.dispose(); login_activity();
                }

            }else{
                error.setForeground(Color.red);
                error.setText("<html>Incorect <b>username</b> or <b>password</b><html>");
            }

        } catch (SQLException ex) {
            System.out.print(ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                rs.close();
                pst.close();
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }                   

Now You can Login and the application will redirect User according to user Level

Leave a Reply

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