0

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?

dzwowt
  • 23
  • 1
  • 6
  • Where is clefs declared and where is it filled? – ihsan kocak Aug 06 '13 at 01:43
  • 1
    It is pretty clear that this is not the real code, and there is serious doubt that it matches the real code closely enough for a proper analysis. Please provide a real SSCCE. – Stephen C Aug 06 '13 at 01:47
  • You seem to be returning the very same `ArrayList` instance from every call of `getFK` (it's declared outside of the method as regular class member). Make the array list a proper local variable of `getFK`. – Dirk Aug 06 '13 at 16:35
  • Now edited all are in same method but I still don't understand what goes wrong. – dzwowt Aug 06 '13 at 18:52

2 Answers2

1

This loop seems to be adding the same thing to alist each time ...

    while(keys.next()) {
          alist.add(clefs.getString("FKCOLUMN_NAME"));  
    }

I can't be sure this this is the problem ('cos you don't tell us what clefs is!) ... but it looks mighty suspicious to me.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Sorry clefs is meant to be resultset keys – dzwowt Aug 06 '13 at 01:42
  • When I display the content of alist with tables with no foreign keys it gives me an empty array result, so it might work correctly I really coudln't find where the problem is – dzwowt Aug 06 '13 at 01:44
  • So what you are saying is that this is not the actual code you are showing us. I don't think there is much point trying to diagnose code that isn't the real code. Please provide a proper SSCCE. – Stephen C Aug 06 '13 at 01:45
  • It is the real code I edited again to the correct form, it was only a mistake while my first edit cause it's my first time posting a question here. sorry for inconvenience. – dzwowt Aug 06 '13 at 01:48
  • Not withstanding that, please provide a proper SCCEE. – Stephen C Aug 06 '13 at 01:49
0

Check out Sets, they're like Lists, but they have the ability to be unique inside the object. I.e. if you have:

Set<Integer> intSet = new HashSet<Integer>()
intSet.add(1);
intSet.add(1);
for (Integer i : intSet) {
     System.out.println(i);
}

The final output should be:

1

Sets don't allow multiple Identical Objects in the same set.

Also, When you were declaring your ArrayLists, be sure to note what kind of an ArrayList each one is, otherwise your IDE should be yelling at you, but I think it defaults to Object. I recommend having a custom model object if you need 3 fields in the Set/List and declare it like this List<CustomModelObject> listOfObjects = new ArrayList<CustomModelObject>(); (see above example if you need an example for Set)

sparks
  • 736
  • 1
  • 9
  • 29