1

For my project i try to get all foreign key in my table using DatabaseMetada Object form jdbc but when i execute the following code the result set is always empty even if my table contain foreign key ?

ResultSet v_resultPrimaryKey = p_metadata.getImportedKeys(null, Tools.getDBName(), "bookmarks_tags");


        if (v_resultPrimaryKey.next()) {
            System.out.println("test");
            v_resultPrimaryKey.beforeFirst();
            v_resultPrimaryKey.beforeFirst();

            while (v_resultPrimaryKey.next()) {
                if (p_att.equals(v_resultPrimaryKey.getString("FKCOLUMN_NAME"))) {
                    v_fk = v_resultPrimaryKey.getString("PKTABLE_NAME") + "."
                           + v_resultPrimaryKey.getString("PKCOLUMN_NAME");
                    v_fkName = v_resultPrimaryKey.getString("FK_NAME");
                }
            }               

            if(!v_fk.equals("")){
                v_foreignKey = new ForeignKey(v_fkName, v_fk);
            }


        }
kazor02x
  • 43
  • 2
  • 8
  • Which database and which driver + version? Also, try using `"BOOKMARKS_TAGS"` as the table name if your database stores unquoted table names in upper case, and `null` for the schema name – Mark Rotteveel Apr 24 '17 at 15:12
  • sgbd : mysql driver : the lat jdbc driver for mysql and i have already try with null for the schema name i will try the uppercase tommorow when i will get back at work – kazor02x Apr 24 '17 at 18:26
  • 1
    _"sgbd : mysql driver : the lat jdbc driver for mysql"_ <- What does that mean? – Mark Rotteveel Apr 25 '17 at 06:54

1 Answers1

0

stumbled upon this and this may help in case anyone may see it

DatabaseMetaData p_metadata= connection.getMetaData();
ResultSet v_resultPrimaryKey = p_metadata.getImportedKeys(null, Tools.getDBName(), "bookmarks_tags");

   if (v_resultPrimaryKey.next()) {
       System.out.println("test");
       v_resultPrimaryKey.beforeFirst();
       v_resultPrimaryKey.beforeFirst();

            while (v_resultPrimaryKey.next()) {
                if (p_att.equals(v_resultPrimaryKey.getString("FKCOLUMN_NAME"))) {
                    String v_fk = v_resultPrimaryKey.getString("PKTABLE_NAME") + "."
                           + v_resultPrimaryKey.getString("PKCOLUMN_NAME");
                    String v_fkName = v_resultPrimaryKey.getString("FK_NAME");
                }
            }               

            if(!v_fk.equals("")){
                v_foreignKey = new ForeignKey(v_fkName, v_fk);
            }


        }
t..
  • 1,101
  • 1
  • 9
  • 22