Problems inserting an OBJ into a LIST

Asked

Viewed 38 times

0

I’m doing a survey using the LIKE, in the MySQL with JSF. Whenever I will insert the search result inside a array to return to my JSF, the array add, but always overwrite 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

1 answer

0


Voce forgot to create Autorvo = new Autorvo(); inside while

Browser other questions tagged

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