save data to an arraylist of a resultset

0

I hope you can help me I am trying to make me return a list from the database through a resultset but I can not get it I hope you can help me thank you

public ArrayList<BeanNivelLlenado> consultarNivel(){
        ArrayList<BeanNivelLlenado> niveles=new ArrayList<BeanNivelLlenado>();
        try {
            conn=this.getConexion();
            st=conn.createStatement();
            rs=st.executeQuery("select * from nivel;");
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();


            while (rs.next()) {

                HashMap row = new HashMap();
                niveles.add(row);
                for(int i=1; i<=columns; i++){
                    row.put(md.getColumnName(i),rs.getObject(i));
                  }
                return niveles;
            }
            this.cerrarConexion();

        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
        return niveles; 
    }

is a select of an html and I would like to know what the error would be, by calling that filled list

<%
    ArrayList<BeanNivelLlenado> listaNivel= (ArrayList<BeanNivelLlenado>)request.getAttribute("listaNivel");
%>
<SELECT NAME="selNivel" SIZE=1 style="width:120px;text-align:center;">
                        <OPTION>Seleccione</OPTION>
                        <%for(BeanNivelLlenado nivel: listaNivel){%>
                        <option ><%= nivel.getNivel()%></option>
                        <%} %>
                </SELECT>
    
asked by Javier Medina 09.06.2018 в 14:18
source

1 answer

0

Try this:

public ArrayList<BeanNivelLlenado> consultarNivel()
{
    List<BeanNivelLlenado> niveles= new ArrayList<BeanNivelLlenado>();
    try {
        conn=this.getConexion();
        st=conn.createStatement();
        rs=st.executeQuery("select * from nivel;");
        ResultSetMetaData md = rs.getMetaData();
        int columns = md.getColumnCount();
        while (rs.next()) {
            // aca tienes que crear un BEAN NIVELES
            BeanNivelLlenado bnll = new BeanNivelLlenado();
            bnll.setNivel(rs.getInt("nivel"));
            bnll.setNombre(rs.getString("nombre"));
            // y así sucesivamente con todos los campos del 
            // bean BeanNivelLlenado
            niveles.add(bnll);
        }
    } catch (Exception e) {
        e.printStackTrace();
        // TODO: handle exception
    } finally {
        this.cerrarConexion();
    }
    return niveles; 
}
    
answered by 09.06.2018 / 14:48
source