title: Get all foreign keys using JDBC
body: <p>I am using postgreSQL. I am trying to get all of the foreign keys from a table. This is the method that I am currently using.</p>
  
  <pre><code>public String getFKeyData(String tableName, int i) throws SQLException {
      DatabaseMetaData dm = connection.getMetaData();
      ResultSet rs = dm.getImportedKeys(null, null, tableName);
      while (rs.next()) {
          fkTableData = rs.getString(i);
      }
      return fkTableData;
  }
  </code></pre>
  
  <p>This code works but it only gets me the last foreign key which is fine if there is only one in the table but this does not fit my needs. All of the examples I have looked at online are very similar to this and only give one foreign key as an output.
  Currently I am just printing the data when a button is pressed.</p>
  
  <pre><code>System.out.println(databaseConnection.getFKeyData(tableName,3));
  System.out.println(databaseConnection.getFKeyData(tableName,4));
  System.out.println(databaseConnection.getFKeyData(tableName,8));
  </code></pre>
  
  <p>3 gets the table the foreign key was imported from. 4 gets the name of the primary key column which is imported. 8 gets the name of foreign key column.
  If anyone can help I would greatly appreciate it.</p>