I'm using a code that was already done by someone, I made some modifications about what I wanted.
This code has 2 types of access "Administrator and Official" would like that when the person entered with the login of type Employee it could not see the following items (jMenu2, jMenuItem12, jMenuItem13) that is in the jframe "Menu" that opens shortly after logging in. Could someone give me strength?
Main class
package Classes;
import Telas.Menu;
import Telas.Login;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Login().setVisible(true);
}
}
SQLConnection class
package Classes;
import java.sql.*;
import javax.swing.*;
/**
*
* @author rafael
*/
public class SQLConnection {
Connection conn = null;
public static Connection java_db() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://DESKTOP-33483CU:1433;databaseName=DADOSPROD;user=PROD");
return conn;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
Login screen
package Telas;
import Classes.SQLConnection;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
/**
*
* @author rafael
*/
public class Login extends javax.swing.JFrame {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
/**
* Creates new form Login
*/
public Login() {
initComponents();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
LoginUsuario.requestFocus();
}
});
conn = SQLConnection.java_db();
Toolkit toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
setLocation(size.width / 2 - getWidth() / 2,
size.height / 2 - getHeight() / 2);
}
private void EntrarActionPerformed(java.awt.event.ActionEvent evt) {
if (LoginUsuario.getText().equals("")) {
JOptionPane.showMessageDialog(null, "O Campo Usuário está vazio");
} else if (LoginSenha.getText().equals("")) {
JOptionPane.showMessageDialog(null, "O Campo Senha está vazio");
} else {
String sql = "select id,username,password,division from Users Where (username =? and password =? and division =?)";
try {
int count = 0;
pst = conn.prepareStatement(sql);
pst.setString(1, LoginUsuario.getText());
pst.setString(2, LoginSenha.getText());
pst.setString(3, txt_divisao.getSelectedItem().toString());
rs = pst.executeQuery();
{
}
while (rs.next()) {
int id = rs.getInt(1);
Emp.empId = id;
count = count + 1;
}
String access = (txt_divisao.getSelectedItem().toString());
if (access.equals("Administrador")) {
if (count == 1) {
JOptionPane.showMessageDialog(null, "Bem Vindo!");
Menu j = new Menu();
j.setVisible(true);
this.dispose();
} else if (count > 1) {
JOptionPane.showMessageDialog(null, "Duplicado, nome de usuário ou acesso à senha negado");
} else {
JOptionPane.showMessageDialog(null, "O nome de usuário e a senha não estão corretos");
}
} else if (access.equals("Funcionario")) {
if (count == 1) {
JOptionPane.showMessageDialog(null, "Bem Vindo!");
Menu j = new Menu();
j.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "O nome de usuário e a senha não estão corretos");
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
Menu Screen
package Telas;
/**
*
* @author rafae
*/
public class Menu extends javax.swing.JFrame {
/**
* Creates new form Menu
*/
public Menu() {
initComponents();
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
teste1 tela = new teste1();
tela.setVisible(true);
dispose();
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
teste2 tela = new teste2();
tela.setVisible(true);
dispose();
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
teste3 tela = new teste3();
tela.setVisible(true);
dispose();
}
Emp class inside screens
package Telas;
/**
*
* @author rafae
*/
class Emp {
static int empId;
}
Back button
Menu tela = new Menu(false); // setei false igual no usuário que você fez.
tela.setVisible(true);
dispose();