简体   繁体   中英

Java Netbeans Login Form

I am a beginner in java.For academic purpose I have to develop a system.In that system in the login form there are three field I have to cover:username,password and selecting user level by using jcombobox.As far as I've done with my project is that I can login with any username,password of the database and I link my jcombobox with the database.But I want to login by giving particular username and password which is stored in database and selecting the particular login level (like login as administrator/manager/engineer) from jcombobox.Only that particular person can login with the username,password and level.Another person of another level have to give their required username,password,level. Here is my code,

import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.sql.*;
import javax.swing.*;

public class LogInJFrame extends javax.swing.JFrame {

Connection conn=null;
ResultSet rs = null;
PreparedStatement pst=null;

public LogInJFrame() {
    initComponents();
    conn=JavaConnector.ConnectorDb();
    Fill_Combo();
}

private void Fill_Combo()
{
    try
    {
        String sql="Select * from Level";
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        while(rs.next()){
        String type=rs.getString("Type");
        cmb_loginAs.addItem(type);
        }
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }
    finally
    {
        try
        {
            rs.close();
            pst.close();

        }
        catch(Exception e)
        {

        }
    }
} 

private void btn_loginActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String sql="select * from Level where UserName =? and Password =?  ";
try{

pst =conn.prepareStatement(sql);
pst.setString(1, txt_username.getText());
pst.setString(2, txt_password.getText());

rs=pst.executeQuery();

if(rs.next())
{
    JOptionPane.showMessageDialog(null, "The Username and Password is correct");
    rs.close();
    pst.close();

    AdministratorLogin ad=new AdministratorLogin();
    ad.setVisible(true);
}
else
{
    JOptionPane.showMessageDialog(null, "The Username and Password is not correct");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
finally
    {
        try
        {
            rs.close();
            pst.close();

        }
        catch(Exception e)
        {

        }
    }
}                                         

private void txt_passwordKeyPressed(java.awt.event.KeyEvent evt) {                                        
    if(evt.getKeyCode()==KeyEvent.VK_ENTER)
    {
        String sql="select * from Level where UserName =? and Password =?  ";
try{

pst =conn.prepareStatement(sql);
pst.setString(1, txt_username.getText());
pst.setString(2, txt_password.getText());

rs=pst.executeQuery();
if(rs.next())
{
    JOptionPane.showMessageDialog(null, "The Username and Password is correct");
    rs.close();
    pst.close();

    AdministratorLogin ad=new AdministratorLogin();
    ad.setVisible(true);
}
else
{
    JOptionPane.showMessageDialog(null, "The Username and Password is not correct");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
finally
    {
        try
        {
            rs.close();
            pst.close();

        }
        catch(Exception e)
        {

        }
    }

    }
}                                                         
}

I've tried a lot but can't figure out exactly what I need to do to get my desired result.Will anyone help me with that please?Thank you.

You could always link the username and password to an account type inside of the DB. Then get that out from the DB store it in a string. then do something like

if(accountType.equals("engineer")) {
     // load specific page
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM