How to delete objects within a List?

0
In this project I'm using JSF, PrimeFaces and I'm still new in programming .. the list screen is displaying information according to the bank

I just wanted to delete these fields that appear on the list screen when selected through the checkbox ...

Here's what I've done so far:

@ManagedBean
public class ControlerBean implements Serializable {

private List<Funcionario> funcionarios = this.listarDados();

private List<Funcionario> funcionarioSelecionados;
// private PreparedStatement stmte;

public List<Funcionario> getFuncionarios() {
    return funcionarios;
}

public void setFuncionarios(List<Funcionario> funcionarios) {
    this.funcionarios = funcionarios;
}

public List<Funcionario> getFuncionarioSelecionados() {
    return funcionarioSelecionados;
}

public void setFuncionarioSelecionados(List<Funcionario> funcionarioSelecionados) {
    this.funcionarioSelecionados = funcionarioSelecionados;
}

public List<Funcionario> listarDados() {

    //gerencia
    String sqq = "select funcionario.cpf , nome, matricula,diretoria,departamento,divisao, cargo "
            + "from funcionario "
            + "left JOIN dadosprofissionais "
            + "ON dadosprofissionais.cpf = funcionario.cpf";

    List<Funcionario> lista = new ArrayList<Funcionario>();

    try {
        Statement tt = Conexao.getConexao().createStatement();
        //PreparedStatement tt = Conexao.getConexao().prepareStatement(sqq);
        //tt.setString(1, sqq);
        ResultSet resul = tt.executeQuery(sqq);

        while (resul.next()) {

            Funcionario funcionario = new Funcionario();
            funcionario.setCpf(resul.getString("cpf"));
            funcionario.setNome(resul.getString("nome"));
            funcionario.setMatricula(resul.getInt("matricula"));
            funcionario.setDiretoria(resul.getString("diretoria"));
            funcionario.setDepartamento(resul.getString("departamento"));
            funcionario.setDivisao(resul.getString("divisao"));
            funcionario.setCargo(resul.getString("cargo"));

            lista.add(funcionario);

        }

    } catch (SQLException ex) {

        Logger.getLogger(ControlerBean.class.getName()).log(Level.SEVERE, null, ex);
        //Logger.getAnonymousLogger(ListarBean.class.getName()).log(Level.SEVERE, null, ex);
    }
    return lista;
}

public void excluir() {

    DeletarCadastro excluir = new DeletarCadastro();
    excluir.deletarFuncionario(funcionarioSelecionados);

    for (int i = 0; i < funcionarioSelecionados.size(); i++) {

        Funcionario p = funcionarioSelecionados.remove(i);
        if (p.getCpf().equals(p)) {

            funcionarioSelecionados.remove(p);

        }

    }

}

My page where you list ... and it has the check box to delete

<p:commandLink
            process="tabela" action="#{controlerBean.excluir()}"  >                         
            <left>
                <h:graphicImage library="imagens" name="lixo.png" width="30" height="30"/>
            </left>                 
        </p:commandLink>


        <p:dataTable id="tabela" var="funcionario" value="#{controlerBean.listarDados()}" selection="#{controlerBean.funcionarioSelecionados}" rowKey="#{funcionario.cpf}" style="margin-bottom:0">

            <f:facet name="header">

            </f:facet>

            <p:ajax event="rowSelect" />

            <p:column selectionMode="multiple" style="width:16px;text-align:center"/>

            <p:column  filterBy="#{funcionario.cpf}" headerText="CPF" footerText="Grande Recife " filterMatchMode="contains">
                <h:outputText value="#{funcionario.cpf}" />
            </p:column>

            <p:column headerText="Nome">
                <h:outputText value="#{funcionario.nome}" />
            </p:column>

            <p:column headerText="Matricula">
                <h:outputText value="#{funcionario.matricula}"/>
            </p:column>

            <p:column headerText="Diretoria">
                <h:outputText value="#{funcionario.diretoria}"/>
            </p:column>

            <p:column  headerText="Gerencia">
                <h:outputText value="#{funcionario.departamento}"/>
            </p:column>

            <p:column  headerText="Divisao">
                <h:outputText value="#{funcionario.divisao}"/>
            </p:column>

            <p:column  headerText="Cargo">
                <h:outputText value="#{funcionario.cargo}"/>
            </p:column>                              

        </p:dataTable>        
        <br/><br/><br/>


    </h:form>

</ui:define>

Class you delete from the bank

public class DeletarCadastro {

public Boolean deletarFuncionario(Funcionario deletar) {
     Boolean retorno = false;
    String sql = "DELETE FROM funcionario WHERE cpf = ?";

    try {
        PreparedStatement stmt = Conexao.getConexao().prepareStatement(sql);
        stmt.setString(1, deletar.getCpf());
        stmt.executeUpdate();
        stmt.close();
    } catch (SQLException ex) {
        Logger.getLogger(DeletarCadastro.class.getName()).log(Level.SEVERE, null, ex);
    }

    return retorno;
}

}

Method that calls the class DeleteCast class ... this method is in my controlerBean

public void excluir(String nome) {

    DeletarCadastro excluir = new DeletarCadastro();
    //excluir.deletarFuncionario(funcionario);



    for (Funcionario func: funcionarios) {

     funcionarios.remove(func);

       excluir.deletarFuncionario((Funcionario) funcionarios);


    }

But it is not deleting the selected parameters from the list - Help aew o /

    
asked by anonymous 10.07.2016 / 01:59

1 answer

0

I'll try to explain how to do ( ñ to finding the checkbox in this code xx) if you identify the checkbox by the name="" attribute, put name="del[]" and for value="#{cpf-ou-id-do-registro}" you must have a controler that manages this data in this control, or in the place that you perform the actions in the database, create a foreach with that array that you will receive from checkbox's :

suaCheckBox = no método post ex: POST['del[]'];

foreach(POST['del[]'] as int cpf){
     Funcionario.setCPF(cpf);
     DeletarCadastro.deletarFuncionario(Funcionario);
}

When you set the value so value="name[]" it interprets as a array (can not remember if in jsf is thus tbm, try ae), then you pass array to foreach and call delete in every loop , I believe it would be better to use id to delete instead of cpf . I think;

    
10.07.2016 / 03:24