I edited the question: I'm trying to store foreign keys of all tables of a database in an arraylist Fkeys. I have the method getFK that retrieves foreign keys (if they exist) of existing tables
public ArrayList Fkeys = new ArrayList();
public ArrayList Vec = new ArrayList();
public ArrayList getFK () {
try {
/* Database connection */
DBConnection connect = new DBConnection();
connect.DBConnect();
Connection con = connect.con;
/* Getting some infos regarding keys*/
DatabaseMetaData metadata = con.getMetaData();
ResultSet clefs = metadata.getImportedKeys(null, null, "persons");
while(clefs.next()) {
Vec.add(clefs.getString("FKCOLUMN_NAME"));
}
Fkeys.add(Vec);
clefs = metadata.getImportedKeys(null, null, "departments");
while(clefs.next()) {
Vec.add(clefs.getString("FKCOLUMN_NAME"));
}
Fkeys.add(Vec);
}
Now when I execute this I get this result : [[ ], [ ]] because the two tables persons and departments have no foreign key. But when I add these lines to the method regarding the table students which has 3 foreign keys
clefs = metadata.getImportedKeys(null, null, "students");
while(clefs.next()) {
Vec.add(clefs.getString("FKCOLUMN_NAME"));
}
Fkeys.add(Vec);
I get this result : [[id, person, depp], [id, person, depp], [id, person, depp]] While it is supposed to be: [[id, person, depp], [ ], [ ]] What I'm doing wrong? and why the content of Vec is stored in all positions of Fkeys?