2
I have a view in my application database where I account for the user’s name, his sector and the total number of records issued (another table’s count):
Table(View) Totalporusuario
Columns: name(string), sector(String), total(int)
I need to store this 3 information in one array and return from model for view and relate in a JTable
, but I can’t imagine how to create a arraylist
multidimensional. I thought about Map but I don’t know how it works.
My current code is (I could only do with 2 columns):
public List getTotal() {
String query = "select nome,setor,total from TotalPorUsuario";
List<ArrayList> lista1 = new ArrayList<>();
ArrayList<String> l2 = new ArrayList<>();
ArrayList<Integer> l3 = new ArrayList<>();
try {
this.con = ConnectionFactory.createConnection();
this.pstm = this.con.prepareStatement(query);
this.r = this.pstm.executeQuery();
while (this.r.next()) {
l2.add(this.r.getString("nome"));
l3.add(this.r.getInt("total"));
}
lista1.add(l2);
lista1.add(l3);
} catch (SQLException ex) {
ex.printStackTrace();
throw new ExcecaoPadrao(ex.getMessage());
} finally {
try {
ConnectionFactory.closeConnection(con, pstm, r);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return lista1;
}
What is the recommended way to return the three columns: in 3 Arraylist
or there is a less complex way using map
?
Diego F , use Object
– Rodrigo Santiago
@Rodrigosantiago did not understand.
– user28595
Arraylist<Object> can contain any type, and even better Arraylist<Object[]> can contain all values recovered by Resultset. This is even true for primary data, java already converts to Integer, Boolean etc.
– Rodrigo Santiago
That I’m already doing, only I have to add one more column, there would be 3 arraylists within the list. I wonder if there is a way to do such a thing with arraylist or map in a more simplified way.
– user28595