1

I have a list of objects (result from a select query), I want to convert it into an array and I want to put each column of the query in a column in the array. I have tried many solutions in the forum but I haven't found a solution.

In my bean I have the following list:

private List<PhBonCmd> listEntree; // The PhBonCmd is an object model imported  has attributes like codeprod , quant ,...
.....
String sql ="select c.codeprod as codeprod , c.quant as quant ,c.date_cmd as date_cmd, c.date_expir as date_expir,c.numbco as numbco, c.auteur as auteur,"
            +"c.idcmd as idcmd ,f.nomfourn as nomfourn ,coalesce(c.quant_livre, 0) as quant_livre ,m.libelle as libelle "
            +"from commandes c,listeproduit m,fournisseur f "
            +"where c.codeprod=m.codeproduit and c.fourn=f.idfourn and c.statut='IN' and c.numbco ='"+getNumbco()+"' ";

   listEntree =  (List<PhBonCmd>) this.bcService.execRequete(sql);//Here the results of the sql query

Now what I want is to put each column of the List (listEntree) in a multidimensional arraylist, so as if I want to access to a specific single value in of the arraylist, I do so.

Draken
  • 3,134
  • 13
  • 34
  • 54
SANTEREK
  • 41
  • 9
  • first get type of result of executed request, after that if it's multi-dimensional then `List` casting is right, otherwise intialize `listEntree = new List` and add each item – The Badak Jun 13 '16 at 15:14
  • Hello I've Found the solution given by Dans http://stackoverflow.com/questions/14773264/convert-listobject-to-string-in-java – SANTEREK Jun 13 '16 at 15:50
  • Solution Founded: http://stackoverflow.com/questions/14773264/convert-listobject-to-string-in-java – SANTEREK Jun 13 '16 at 15:53

3 Answers3

0

Why are you casting to List<PhBonCmd>? . The ResultSet is by default multidimensional. If you want to convert to a multidimensional List, you can do it by processing the ResultSet.

If you are using Spring , you can use RowMapper or ResultSetExtractor to get the desired behaviour.

Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
0

I want to put the results of the query in an array and from there , i want to access to elements of the array . Have things like this array1[row][col]

SANTEREK
  • 41
  • 9
0

I don't know the reason of doing that, when you have OOP power, but here it is. You have to use reflection to get know fields (lets say columns if you wish) of PhBonCmd on fly:

Class clazz = PhBonCmd.class;
Field[] fields = clazz.getFields();

String[][] myDemensionalArray = new String[fields.length()][listEntree.size()]; 
for(int i=0; i < fields.length(); i++){
    for(int j=0; j < listEntree.size(); j++){
              myDemensionalArray[i][j] = fields[i].get(listEntree.get(j));
        }
} 
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192