I have a view in the bank of my application where I count the user's name, his sector and the total number of records issued (tale from another table):
TotalPorUsuario (View) Table
Columns: name (string), sector (String), total (int)
I need to store these 3 information in an array and return from model to view and relate to a JTable
, but I can not imagine how to create a% multi-dimensional%. I thought about Map but I do not know how it works.
My current code is (I could only do it 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 is there a less complex form using Arraylist
?