I need to list an array to display it in a table (specifically the user table with its attributes), I am using JSTL to display the data in the JSP, the only problem is that it only lists the first object of my ArrayList, here is 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 position 1 or higher of my array to read the data, it sends me the following error (which is because the arrayList only has 1 element, I know)
In the main I would work it in the following way, intuiting that in User_DTO_bean there are the accessor methods, the getters and setters.
You could close the
reader
outside of thewhile
, so it doesn't make senseyou could create a for-each to be able to visualize the content of your arrayList and even more so if it is from Object, you have to do it yes or yes