0
I’m using a code that was already made by someone, I made some modifications regarding what I wanted.
This code has 2 types of "Administrator and Employee" access that you would like when you login with the Employee type to be unable to see the following items (jMenu2,jMenuItem12,jMenuItem13) that are in the "Menu" jframe that opens right after you login. Someone could give me a boost?
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);
}
}
Class Sqlconnection
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;
}
}
}
Tela Login
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();
Please add a [mcve] so you can test the code.
– user28595
Missing menu items that are composed of a menu bar and some menu items. I will post an example
– Rafael Chaves
Well, since you’re not being able to create a simple, no-dependency version of your code, I can post a totally different example with a solution and you can adapt it to your code there, it can be?
– user28595
pole I’ll take the test
– Rafael Chaves
Hello Are you using a database? if yes create a table for login, so every login done will be stored in this table so you can do the last login search done and compare if the type of user is Adm or common, making this comparison you can disable the buttons you want.
– Wagner elias de Andrade
@Wagnereliasdeandrade, yes, I already have this table for login and now is working my code. I just have a problem that I explained in the last answer
– Rafael Chaves