Problems inserting an OBJ into a LIST

0

I'm doing a search using LIKE , MySQL with JSF . Whenever I enter the search result within a array to return to my JSF , array adds, but always overwrites items with the last inserted.

My code

Bean :

public DataModel<AutorVO> getAutorDataModelID() 
{

    AutorDAO dao = new AutorDAO();

    try {
        List<AutorVO> listaAutorId = dao.listaID(vo);
        autorDataModelID = new ListDataModel<AutorVO>(listaAutorId);

        return autorDataModelID;
    } catch (Exception e) {

    }
    return autorDataModelID;
}

public void setAutorDataModelID(DataModel<AutorVO> autorDataModelID) {
    this.autorDataModelID = autorDataModelID;
}

View :

<h:form>
    <h:commandButton id="btBuscar" value="Buscar"  action="#{autorBean.buscaID()}">
    </h:commandButton>
    <h:inputText value="#{autorBean.vo.pesquisa_nome}" id="tt"></h:inputText>
    <h:dataTable  id="tbId" value="#{autorBean.autorDataModelID}" var="dados1" 
    border="1" cellpadding="10">
        <h:column>
            <f:facet name="header">
                <h:outputText value="ID"></h:outputText>
            </f:facet>
            <h:outputText value="#{dados1.id}"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputText value="Nome"></h:outputText>
            </f:facet>
            <h:outputText value="#{dados1.nome}"></h:outputText>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputText value="Nome Artistico"></h:outputText>
            </f:facet>
            <h:outputText value="#{dados1.nome_artistico}"></h:outputText>
        </h:column>
        <h:column >
            <f:facet name="header">
                <h:outputText value="Ações"></h:outputText>                                               
            </f:facet>
            <h:commandButton value="Deletar" action="deleta_autor?faces-redirect=true"
              actionListener="#{autorBean.selecionaReg1()}">
            </h:commandButton>

            <h:commandButton value="Editar" action="editar_autor?faces-redirect=true"
             actionListener="#{autorBean.selecionaReg1()}">
            </h:commandButton>
        </h:column>
    </h:dataTable>
</h:form>

DAO

public List<AutorVO> listaID(AutorVO vo) throws ClassNotFoundException, 
                                                 SQLException 
{
    String SQL = "Select * from autor where nome like ? ";
    PreparedStatement pstm = getConn().prepareStatement(SQL);
    pstm.setString(1, '%' + vo.getPesquisa_nome() + '%');
    ResultSet rs = pstm.executeQuery();
    List<AutorVO> autorId = new ArrayList<AutorVO>();
    while (rs.next()) {

        vo.setId(rs.getInt("id"));
        vo.setNome(rs.getString("nome"));
        vo.setNome_artistico(rs.getString("nome_artistico"));
        vo.setData_nascimento(rs.getInt("data_nascimento"));

        autorId.add(vo) ;

    }

    return autorId;
}

Remembering that the array is overwriting everything

    
asked by anonymous 23.11.2016 / 00:59

1 answer

0

You forgot to create AutorVO vo = new AutorVO (); within the while

    
23.11.2016 / 14:22