I need to list an arrayList in java, but only list the first element of my array

0

I need to list an array to show it in a table (specifically the user table with its attributes), I'm using JSTL to show the data in the JSP, the only problem is that it only lists the first object of my ArrayList, here I leave the code of the function that generates the ArrayList.

public List<Usuario_DTO_bean> listarUsuarios() {
    List<Usuario_DTO_bean> listaUsuario= new ArrayList<>();
    try {
        con=Conector.connect();
        String sql = "select * from usuario";
        Statement statement=con.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while(resultSet.next()) {
            int id = resultSet.getInt("id");
            String usu = resultSet.getString("usuario");
            String clave = resultSet.getString("clave");
            String permisos=resultSet.getString("permisos");
            int estado=resultSet.getInt("estado");

            Usuario_DTO_bean usuario_DTO_bean=new Usuario_DTO_bean(id, usu, clave, permisos, estado);
            listaUsuario.add(usuario_DTO_bean); 
            resultSet.close();
            statement.close();
            con.close();
            System.out.println("nuevo registro");
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    return listaUsuario;
}

When I call to print the first data by console there is no problem, the following is the code that is in a main class that I use to show data by console

Usuario_DTO_bo listar=new Usuario_DTO_bo();
    List<Usuario_DTO_bean> lista=new ArrayList<>();
    lista=listar.listarUsuarios();
    int tamano=lista.size();
    String aux=lista.get(0).getUsuario();
    System.out.println(aux);
    System.out.println(tamano);

Result by console

When I access the position 1 or higher of my array to read the data it sends me the second error (which is because the arrayList only has 1 element I know)

    
asked by angelo1793 18.07.2018 в 18:18
source

3 answers

0

You could close the reader out of while , so it does not make sense

public List<Usuario_DTO_bean> listarUsuarios() {
    List<Usuario_DTO_bean> listaUsuario= new ArrayList<>();
    try {
        con=Conector.connect();
        String sql = "select * from usuario";
        Statement statement=con.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while(resultSet.next()) {
            int id = resultSet.getInt("id");
            String usu = resultSet.getString("usuario");
            String clave = resultSet.getString("clave");
            String permisos=resultSet.getString("permisos");
            int estado=resultSet.getInt("estado");

            Usuario_DTO_bean usuario_DTO_bean=new Usuario_DTO_bean(id, usu, clave, permisos, estado);
            listaUsuario.add(usuario_DTO_bean); 

            System.out.println("nuevo registro");
        }
             resultSet.close();
            statement.close();
            con.close();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    return listaUsuario;
}
    
answered by 18.07.2018 / 18:30
source
1
public List<Usuario_DTO_bean> listarUsuarios() {
List<Usuario_DTO_bean> listaUsuario= new ArrayList<>();
try {
    con = Conector.connect();
    String sql = "select * from usuario";
    Statement st = con.createStatement();
    ResultSet rs = statement.executeQuery(sql);
    while(resultSet.next()) {            
        int id = resultSet.getInt("id");
        String usu = resultSet.getString("usuario");
        String clave = resultSet.getString("clave");
        String permisos=resultSet.getString("permisos");
        int estado=resultSet.getInt("estado");

        Usuario_DTO_bean usuario_DTO_bean=new Usuario_DTO_bean(id, usu, clave, permisos, estado);
        listaUsuario.add(usuario_DTO_bean); 

        System.out.println("nuevo registro");
    }
        /*Se cierra siempre al ultimo*/
        resultSet.close();
        statement.close();
        con.close();
        /*Deberias de importar la excepcion SQLException*/
} catch (Exception e) {
    System.err.println(e.getMessage());
}finally{
/*Es una buena practica agregar el finally, por lo general aquí se cierra
La conexion a la BD algo como Conector.desconectar()*/
}
return listaUsuario;}

In the main I would work it in the following way, intuiting that in User_DTO_bean are the accessor methods, the getters and setters.

public static void main(String args[]){
       Usuario_DTO_bo user = new Usuario_DTO_bo();
       List<Usuario_DTO_bean> listaUsuarios = user.listarUsuarios();
       for(Usuario_DTO_bean usuario: listaUsuarios){
//Imprimes cada atributo del usuario, por poner un ejemplo:
          System.out.println("Usuario: " +usuario.getNombre());
       }
    
answered by 18.07.2018 в 19:05
0

you could create a for-each to be able to visualize the content of your arrayList and even more if it is of Object, you have to do it if or if

    
answered by 18.07.2018 в 23:09