How to delete objects within a List?

Asked

Viewed 189 times

0

Guys ,is the following... In this project I’m using JSF,Primefaces and I’m still new to programming.. the list screen is displaying the information according to the bank

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

Follow 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 lists... and on 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 excluded from the seat

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 deletarCadastro...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);


    }

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

  • at least give me a light...I don’t know what I do anymore..

1 answer

0

I’ll try to explain how to do (ñ to find the checkbox in this code x-x) if you identify the checkbox by the attribute name="" put it like this name="del[]" and to the value="#{cpf-ou-id-do-registro}" you must have a controler that manages this data, this control, or the place 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 define value so value="name[]" he interprets as a array (I don’t remember if in the jsf is so tbm, try ae), then you pass a array to the foreach and calls the delete in each loop, I believe it would be better to use the id to delete instead of cpf. I guess;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.