Gustavo
Gustavo

Reputation: 17

Compare value of SQL query

Need to compare the value obtained in my SQL query but the attribute that want to compare has N results that are: "Administrador", "Financeiro" and "Operacional". It was to run the IF in the code below, but I'm doing some wrong comparison and is running ELSE.

public void Logar(){

    String sql = "Select usuario_login, senha_login, nome_login, nivelAcesso from nivelAcessoSistema natural join login group by nivelAcesso having usuario_login = ? and senha_login = ?";

    try{

        pst = con.prepareStatement(sql); 

        pst.setString(1, txtUsuario.getText()); //Campo do usuário
        pst.setString(2, txtSenha.getText()); //Campo da senha

        rs = pst.executeQuery(); 

        if(rs.next()){ //Se existe registro no banco de dados com os dados informados na tela de login...

            String nivelAcesso = rs.getString("nivelAcesso"); //Comparando valor de atríbuto "nivelAcesso" do banco de dados

            if(nivelAcesso.equals("Administrador") && nivelAcesso.equals("Financeiro") && nivelAcesso.equals("Operacional")){

                JOptionPane.showMessageDialog(null, "OK");
            }

            else{
            JOptionPane.showMessageDialog(null, "Wrong");
            }

I hope you can help me on this.

Thank you.

Upvotes: 0

Views: 1649

Answers (6)

Gustavo
Gustavo

Reputation: 17

I signed up a user in the database with nivelAcesso = "Operacional" and "Financeiro" but I can not access the Set NIVELACESSOOperFin Everytime I try to run the Set NIVELACESSOOperFin is running NIVELACESSOOper that only have nivelAcesso = "Operacional" See the code:

Set<String> NIVELACESSOOperFin; {

    NIVELACESSOOperFin = new HashSet<String>();
    NIVELACESSOOperFin.add("Operacional");
    NIVELACESSOOperFin.add("Financeiro");
}

Set<String> NIVELACESSOOper; {

    NIVELACESSOOper = new HashSet<String>();
    NIVELACESSOOper.add("Operacional");
}

Set<String> NIVELACESSOAdmFinOper; {

    NIVELACESSOAdmFinOper = new HashSet<String>();
    NIVELACESSOAdmFinOper.add("Administrador");
    NIVELACESSOAdmFinOper.add("Financeiro");
    NIVELACESSOAdmFinOper.add("Operacional");
}


public void Logar() {

    Set<String> result = new HashSet<String>();

    String sql = "SELECT usuario_login, senha_login, nome_login, nivelAcesso FROM nivelAcessoSistema NATURAL JOIN login GROUP BY usuario_login HAVING usuario_login = ? AND senha_login = ?";

    try {

        pst = con.prepareStatement(sql);

        pst.setString(1, txtUsuario.getText()); //Campo do usuário
        pst.setString(2, txtSenha.getText()); //Campo da senha

        rs = pst.executeQuery();

        if (rs.next()) { //Se existe registro no banco de dados com os dados informados na tela de login...

            String nivelAcesso = rs.getString("nivelAcesso"); //Comparando valor de atríbuto "nivelAcesso" do banco de dados

            result.add(nivelAcesso);

            if (NIVELACESSOOper.containsAll(result)) {

                JOptionPane.showMessageDialog(null, "Operacional");
            }

            else if(NIVELACESSOOperFin.containsAll(result)){

                JOptionPane.showMessageDialog(null, "Operacional - Financeiro");
            }

            else if (NIVELACESSOAdmFinOper.containsAll(result)) {

                JOptionPane.showMessageDialog(null, "Operacional - Financeiro - Administrativo");
            }

Upvotes: 0

Mohit Garg
Mohit Garg

Reputation: 86

@Gustavo, can you try something like this and checks if this helps you?

static Set<String> valueSet;

static {
    valueSet = new HashSet<String>();
    valueSet.add("Administrador");
    valueSet.add("Financeiro");
    valueSet.add("Operacional");
}

public void Logar() {
    Set<String> result = new HashSet<String>();

    String sql = "Select usuario_login, senha_login, nome_login, nivelAcesso from nivelAcessoSistema natural join login group by nivelAcesso having usuario_login = ? and senha_login = ?";

    try {

        pst = con.prepareStatement(sql);

        pst.setString(1, txtUsuario.getText()); //Campo do usuário
        pst.setString(2, txtSenha.getText()); //Campo da senha

        rs = pst.executeQuery();

        while (rs.next()) { //Se existe registro no banco de dados com os dados informados na tela de login...

            String nivelAcesso = rs.getString("nivelAcesso"); //Comparando valor de atríbuto "nivelAcesso" do banco de dados

            result.add(nivelAcesso);
        }

        if (valueSet.containsAll(result)) {

            JOptionPane.showMessageDialog(null, "OK");
        }

        else {
            JOptionPane.showMessageDialog(null, "Wrong");
        }
    }

Upvotes: 0

Mohit Garg
Mohit Garg

Reputation: 86

@Gustavo, It looks like you want to compare the value that you got from database, and values will be just any one of these "Administrador" or "Financeiro" or "Operacional". So, to compare these, you need to use "||" rather than "&&" in your IF condition. For example: query

"Select usuario_login, senha_login, nome_login, nivelAcesso from nivelAcessoSistema natural join login group by nivelAcesso having usuario_login = ? and senha_login = ?"

returns nivelAcesso="Administrador" then your code will be like:

if(nivelAcesso.equals("Administrador") || nivelAcesso.equals("Financeiro") || nivelAcesso.equals("Operacional")){

            JOptionPane.showMessageDialog(null, "OK");
        }

In this case, it will result true and will enter in IF block.

Upvotes: 0

Dewfy
Dewfy

Reputation: 23614

OPTION 1 You can use fixed set of constants or even apply enum and use EnumSet

For example:

static final Set<String> importantRoleSet = new HashSet<String(){{
    add("Administrador");
    add("Financeiro");
    ....
}};

After that in your code:

...
if( importantRoleSet.contains(nivelAcesso) ){
   //do the role-related operations
}

OPTION 2

Place this logic to SQL and select already defined 0/1 for correct role:

SELECT nivelAcesso in ('Administrador', 'Financeiro') as isInRole FROM ...

In this case you create new field isInRole - that will contain 0 or 1 depending if nivelAcesso is one of permitted values

Upvotes: 0

The problem resides here:

     if(nivelAcesso.equals("Administrador") && nivelAcesso.equals("Financeiro")
 && nivelAcesso.equals("Operacional"))

nivelAcesso can't be equals to all these values at same time. Change this line to:

     if(nivelAcesso.equals("Administrador") || nivelAcesso.equals("Financeiro") 
|| nivelAcesso.equals("Operacional"))

for more information about logic comparisons, please read this:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

Upvotes: 2

splrs
splrs

Reputation: 2432

if(nivelAcesso.equals("Administrador") && nivelAcesso.equals("Financeiro") && nivelAcesso.equals("Operacional")){

should be

if(nivelAcesso.equals("Administrador") || nivelAcesso.equals("Financeiro") || nivelAcesso.equals("Operacional")){

You need the OR rather than the AND operator:

nivelAcesso.equals("Administrador")

and

nivelAcesso.equals("Financeiro")

can never both be true at the same time.

Upvotes: 0

Related Questions