title: How to get primary keys for all tables in JDBC?
body: <p>I have a database with at least 500 tables. What is the exact code to get all the primary keys and foreign keys of each table?</p>
  
  <pre><code>//Primary Key
  DatabaseMetaData meta=conn.getMetaData();
  ResultSet rs1= meta.getTables(null, null, "TableName" , new String[]{"TABLE"});
  rs1=meta.getPrimaryKeys(null, null, "TableName");
  while(rs1.next())
      System.out.println("Primary Key :"+rs1.getString(4));
  
  //Foreign Key
  rs1=meta.getExportedKeys(null, null, "TableName");
  while(rs1.next())
      System.out.println("Foreign Key :"+rs1.getString(4));
  </code></pre>
  
  <p>I have used this code and it gives me accurate keys but for 500 tables, I have to change my code 500 times. Is there any way to minimize this effort?</p>