je travaille sur l'exemple à http://java.sun.com/docs/books/tutorial/uiswing/examples/components/GenealogyExampleProject/src/components/GenealogyExample.javadonnées JTree base de données
Je voulais le personnaliser en quelque sorte les données sont tirées d'une table à la place (folder_id, FOLDER_NAME, id_parent [clé étrangère pour déterminer parent].
Voici mon code
public Person getGenealogyGraph() throws SQLException {
Connection con=null;
Statement st=null;
ResultSet rs=null;
String driver ="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/";
String db="java";
con=DriverManager.getConnection(url + db, "root", "");
ArrayList list=new ArrayList();
try {
String sql="Select * from folder";
st=con.createStatement();
rs=st.executeQuery(sql);
list.add("Current Folders");
while(rs.next()) {
String folderName = rs.getString("folder_name");
list.add(folderName);
// System.out.println(folderName);
//Person a1 = new Person(folderName);
}
} catch (Exception e) {
System.out.println(e);
}
rs.close();
st.close();
con.close();
Object hierarchy[]=list.toArray();
for(int i=1; i<hierarchy.length; i++) {
Person a+i=new Person(hierarchy[i]);
}
Si je hardcode comme personne a1 = new Person ("Jack (grand-grand-papa)")., il fonctionne très bien mais je voulais le mettre dans un boucle, avec la variable i: -
Object hierarchy[]=list.toArray();
for(int i=1; i<hierarchy.length; i++) {
Person a+i=new Person(hierarchy[i]);
}
Comment puis-je combiner la variable i et le «a»? En PHP, nous les combinons généralement avec un "." , par exemple "a". $ i;
Merci :) Tout autre exemple sur la création d'arbres à partir de la base de données est également très apprécié. Merci d'avance
Merci pour la réponse. Il semble que hashmap soit la solution :). – Sylph