简体   繁体   English

两个以相同数据开头的ArrayList

[英]Two ArrayLists starting with the same data

Would you like to help me on the following: 您想在以下方面为我提供帮助:

Created 3 ArrayLists with defined values and an ArrayList that will receive the value of a select in the database. 创建了3个具有定义值的ArrayList和一个将在数据库中接收选择值的ArrayList。 Then I want to compare the value of ArrayList called database with ArrayLists with defined values that I created. 然后,我想将数据库的ArrayList值与具有我创建的定义值的ArrayLists进行比较。

Is giving error because the ArrayList called NIVELACESSOOper has only data: "Operacional" and ArrayList called NIVELACESSOOperFin has 2 data that are "Operacional" and "Financeiro" and when I try to run the ArrayList called NIVELACESSOOperFin is running the NIVELACESSOOper because both begin with the data "Operacional "... even I using .containsAll the comparison went wrong because it is comparing the first data and not the whole of the ArrayList called NIVELACESSOOperFin. 正在给出错误,因为名为NIVELACESSOOper的ArrayList仅具有数据:“ Operacional”和名为NIVELACESSOOperFin的ArrayList具有2个数据“ Operacional”和“ Financeiro”,并且当我尝试运行名为NIVELACESSOOperFin的ArrayList时正在运行NIVELACESSOOper,因为两者都以NIVELACESSOOper开头数据“ Operacional” ...即使我使用.contains,所有比较也都出错了,因为它是在比较第一个数据,而不是整个名为NIVELACESSOOperFin的ArrayList。 Continues only comparing the first data. 仅继续比较第一个数据。

I would love to have your support !!! 我希望得到您的支持!

    List NIVELACESSOOper; {

    NIVELACESSOOper = new ArrayList();
    NIVELACESSOOper.add("Operacional");
}

List NIVELACESSOOperFin; {

    NIVELACESSOOperFin = new ArrayList();
    NIVELACESSOOperFin.add("Operacional");
    NIVELACESSOOperFin.add("Financeiro");
}

List NIVELACESSOAdmFinOper; {

    NIVELACESSOAdmFinOper = new ArrayList();
    NIVELACESSOAdmFinOper.add("Administrador");
    NIVELACESSOAdmFinOper.add("Financeiro");
    NIVELACESSOAdmFinOper.add("Operacional");
}


public void Logar() {

    List result = new ArrayList();

    String sql = "SELECT usuario_login, senha_login, nome_login, nivelAcesso FROM nivelAcessoSistema NATURAL JOIN login WHERE 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 (result.containsAll(NIVELACESSOOper)) {

                JOptionPane.showMessageDialog(null, "Enter the screen Operacional");
            }

            else if(result.containsAll(NIVELACESSOOperFin)) {

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

            else if (result.containsAll(NIVELACESSOAdmFinOper)) {

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

It looks like you should change the order of your if statements to start with the largest list : 看起来您应该更改if语句的顺序以从最大的列表开始:

        if (result.containsAll(NIVELACESSOAdmFinOper)) {

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

        else if(result.containsAll(NIVELACESSOOperFin)) {

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

        else if (result.containsAll(NIVELACESSOOper)) {

            JOptionPane.showMessageDialog(null, "Enter the screen Operacional");
        }

That said, your result list can only contain a single element, so only result.containsAll(NIVELACESSOOper) can return true. 也就是说,您的result列表只能包含一个元素,因此只有result.containsAll(NIVELACESSOOper)可以返回true。

I'm not entirely sure what data you are reading from the DB, since the names are not in English, but if you expect multiple rows, you should change your code to : 由于名称不是英语,因此我不确定是否要从数据库读取什么数据,但是如果您希望有多行,则应将代码更改为:

    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);
    }

And only after processing all the rows, you check the result list. 并且只有在处理完所有行之后,才检查result列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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