Cmt redimentionner un tableau (java)

Résolu/Fermé
fhredim - 5 mai 2007 à 12:45
 fhredim - 5 mai 2007 à 14:54
Bonjour,

Cmt redimentionner le tableau TAB pour qu'il s'ajuste au contenu de la table VEHICULE ?
Je veux ranger le résultat de la requete dans un tableau (TAB) initialement vide pour le retourner par la suite


Voici le code :
_______________________________________________________
public static Vehicule [] rechercherVehicule()
{
Vehicule [] TAB = new Vehicule [1]; /*je l'ai initialisé à 1 comme il fallait le dimentionner*/

try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection cox = DriverManager.getConnection("jdbc:derby://localhost:1527/Application", "toto", "toto");
Statement stm = cox.createStatement();

String query = "select * from \"toto\".\"VEHICULE\"" ;
ResultSet rst = stm.executeQuery(query);

/* J'ai essayé : */
TAB.length = rst.getRow();
/* il me dit : connot assign a value to final variable length !!! */

int i=0;
while(rst.next())
{
int id = rst.getInt(1);
String marque = rst.getString(2);
String modele = rst.getString(3);
int prix = rst.getInt(4);
String couleur = rst.getString(5);
TAB[i++] = new Vehicule(id, marque, modele, prix, couleur);
}

cox.close();
}
catch(Exception ex){ ex.printStackTrace();}
return TAB;
}
___________________________________

Que faut-il faire ?

D'avance merci
A voir également:

1 réponse

Eh ben, j'ai pu résoudre mon problème en me servant d'un arraylist.
Merci
______________________________________
public static Vehicule [] rechercherVehiculeParMarque(String marque)
{

ArrayList lst = new ArrayList();

try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection cox = DriverManager.getConnection("jdbc:derby://localhost:1527/Application", "toto", "toto");
Statement stm = cox.createStatement();

String query = "select * from \"toto\".\"VEHICULE\"" ;
System.out.println("Query : " + query);
ResultSet rst = stm.executeQuery(query);
int a = rst.getRow();



int i=0;
while(rst.next())
{
int id = rst.getInt(1);
String mark = rst.getString(2);
String modele = rst.getString(3);
int prix = rst.getInt(4);
String couleur = rst.getString(5);
lst.add(new Vehicule(id, mark, modele, prix, couleur));
}

cox.close();
}
catch(Exception ex){ System.out.println("Lerreur : \n"); ex.printStackTrace();}

Vehicule x;
Vehicule [] tab = new Vehicule [lst.size()];
for(int i=0; i<lst.size(); i++)
{
tab[i] = (Vehicule)lst.get(i);
}
return tab;
}

________________________
0