title: JDBC: Unable to get composite index for my table
body: <p>I am trying to list all the indexes for all the tables in my DB (using a generic solution which is not specific to an RDBMS)</p>
  
  <p>To test out my solution, I have 2 tables in an MS SQL Server DB <code>tempdb</code></p>
  
  <ul>
  <li>Stores </li>
  <li>Staffs</li>
  </ul>
  
  <p>Each table has a PK and the <code>staffs</code> table also have a Unique key column called <code>email</code>.
  There is a composite index on the <code>stores</code> table on <code>store_id, store_name</code>.</p>
  
  <p>Following are the indexes (screenshots from DBeaver)</p>
  
  <p><a href="https://i.stack.imgur.com/hGjau.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hGjau.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/d8oRQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/d8oRQ.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/LwsMV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LwsMV.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/MDSsM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MDSsM.png" alt="enter image description here"></a></p>
  
  <p>The code snippet is as follows</p>
  
  <pre><code>.
  .
  .
  System.out.println("**********************************************************");
  System.out.println("schema &gt;&gt;&gt; "+schema);
  System.out.println("table &gt;&gt;&gt; "+table);
  ResultSet indexValues = tableMeta.getIndexInfo(null, null, table, true, false);
  while (indexValues.next()) {
      ResultSetMetaData rsmd = indexValues.getMetaData();  
      String idxName = indexValues.getString("INDEX_NAME");
      System.out.println("=====================================================");
      System.out.println("rs1 &gt;&gt;&gt; "+indexValues.getString(1));
      System.out.println("rs2 &gt;&gt;&gt; "+indexValues.getString(2));
      System.out.println("rs3 &gt;&gt;&gt; "+indexValues.getString(3));
      System.out.println("rs4 &gt;&gt;&gt; "+indexValues.getString(4));
      System.out.println("rs5 &gt;&gt;&gt; "+indexValues.getString(5));
      System.out.println("rs6 &gt;&gt;&gt; "+indexValues.getString(6));
      System.out.println("rsmd &gt;&gt;&gt; "+rsmd.getColumnCount());
      System.out.println("INDEX_NAME &gt;&gt;&gt; "+indexValues.getString("INDEX_NAME"));
      System.out.println("INDEX_QUALIFIER &gt;&gt;&gt; "+indexValues.getString("INDEX_QUALIFIER"));
      System.out.println("NON_UNIQUE &gt;&gt;&gt; "+indexValues.getBoolean("NON_UNIQUE"));
      System.out.println("TYPE &gt;&gt;&gt; "+indexValues.getShort("TYPE"));
      System.out.println("ORDINAL_POSITION &gt;&gt;&gt; "+indexValues.getString("ORDINAL_POSITION"));
      System.out.println("COLUMN_NAME &gt;&gt;&gt; "+indexValues.getString("COLUMN_NAME"));
      System.out.println("ASC_OR_DESC &gt;&gt;&gt; "+indexValues.getString("ASC_OR_DESC"));
      System.out.println("FILTER_CONDITION &gt;&gt;&gt; "+indexValues.getString("FILTER_CONDITION"));
      System.out.println("TABLE_SCHEM &gt;&gt;&gt; "+indexValues.getString("TABLE_SCHEM"));
      System.out.println("TABLE_CAT &gt;&gt;&gt; "+indexValues.getString("TABLE_CAT"));
      System.out.println("=====================================================");
  
  }
  </code></pre>
  
  <p>The output is as follows (the compound/composite index is not returned)</p>
  
  <pre><code>**********************************************************
  schema &gt;&gt;&gt; dbo
  table &gt;&gt;&gt; stores
  =====================================================
  rs1 &gt;&gt;&gt; tempdb
  rs2 &gt;&gt;&gt; dbo
  rs3 &gt;&gt;&gt; stores
  rs4 &gt;&gt;&gt; null
  rs5 &gt;&gt;&gt; null
  rs6 &gt;&gt;&gt; null
  rsmd &gt;&gt;&gt; 13
  INDEX_NAME &gt;&gt;&gt; null
  INDEX_QUALIFIER &gt;&gt;&gt; null
  NON_UNIQUE &gt;&gt;&gt; false
  TYPE &gt;&gt;&gt; 0
  ORDINAL_POSITION &gt;&gt;&gt; null
  COLUMN_NAME &gt;&gt;&gt; null
  ASC_OR_DESC &gt;&gt;&gt; null
  FILTER_CONDITION &gt;&gt;&gt; null
  TABLE_SCHEM &gt;&gt;&gt; dbo
  TABLE_CAT &gt;&gt;&gt; tempdb
  =====================================================
  =====================================================
  rs1 &gt;&gt;&gt; tempdb
  rs2 &gt;&gt;&gt; dbo
  rs3 &gt;&gt;&gt; stores
  rs4 &gt;&gt;&gt; 0
  rs5 &gt;&gt;&gt; stores
  rs6 &gt;&gt;&gt; PK__stores__A2F2A30C0F20F60B
  rsmd &gt;&gt;&gt; 13
  INDEX_NAME &gt;&gt;&gt; PK__stores__A2F2A30C0F20F60B
  INDEX_QUALIFIER &gt;&gt;&gt; stores
  NON_UNIQUE &gt;&gt;&gt; false
  TYPE &gt;&gt;&gt; 1
  ORDINAL_POSITION &gt;&gt;&gt; 1
  COLUMN_NAME &gt;&gt;&gt; store_id
  ASC_OR_DESC &gt;&gt;&gt; A
  FILTER_CONDITION &gt;&gt;&gt; null
  TABLE_SCHEM &gt;&gt;&gt; dbo
  TABLE_CAT &gt;&gt;&gt; tempdb
  =====================================================
  **********************************************************
  schema &gt;&gt;&gt; dbo
  table &gt;&gt;&gt; staffs
  =====================================================
  rs1 &gt;&gt;&gt; tempdb
  rs2 &gt;&gt;&gt; dbo
  rs3 &gt;&gt;&gt; staffs
  rs4 &gt;&gt;&gt; null
  rs5 &gt;&gt;&gt; null
  rs6 &gt;&gt;&gt; null
  rsmd &gt;&gt;&gt; 13
  INDEX_NAME &gt;&gt;&gt; null
  INDEX_QUALIFIER &gt;&gt;&gt; null
  NON_UNIQUE &gt;&gt;&gt; false
  TYPE &gt;&gt;&gt; 0
  ORDINAL_POSITION &gt;&gt;&gt; null
  COLUMN_NAME &gt;&gt;&gt; null
  ASC_OR_DESC &gt;&gt;&gt; null
  FILTER_CONDITION &gt;&gt;&gt; null
  TABLE_SCHEM &gt;&gt;&gt; dbo
  TABLE_CAT &gt;&gt;&gt; tempdb
  =====================================================
  =====================================================
  rs1 &gt;&gt;&gt; tempdb
  rs2 &gt;&gt;&gt; dbo
  rs3 &gt;&gt;&gt; staffs
  rs4 &gt;&gt;&gt; 0
  rs5 &gt;&gt;&gt; staffs
  rs6 &gt;&gt;&gt; PK__staffs__1963DD9CCB589A48
  rsmd &gt;&gt;&gt; 13
  INDEX_NAME &gt;&gt;&gt; PK__staffs__1963DD9CCB589A48
  INDEX_QUALIFIER &gt;&gt;&gt; staffs
  NON_UNIQUE &gt;&gt;&gt; false
  TYPE &gt;&gt;&gt; 1
  ORDINAL_POSITION &gt;&gt;&gt; 1
  COLUMN_NAME &gt;&gt;&gt; staff_id
  ASC_OR_DESC &gt;&gt;&gt; A
  FILTER_CONDITION &gt;&gt;&gt; null
  TABLE_SCHEM &gt;&gt;&gt; dbo
  TABLE_CAT &gt;&gt;&gt; tempdb
  =====================================================
  =====================================================
  rs1 &gt;&gt;&gt; tempdb
  rs2 &gt;&gt;&gt; dbo
  rs3 &gt;&gt;&gt; staffs
  rs4 &gt;&gt;&gt; 0
  rs5 &gt;&gt;&gt; staffs
  rs6 &gt;&gt;&gt; UQ__staffs__AB6E6164E6F8EDB6
  rsmd &gt;&gt;&gt; 13
  INDEX_NAME &gt;&gt;&gt; UQ__staffs__AB6E6164E6F8EDB6
  INDEX_QUALIFIER &gt;&gt;&gt; staffs
  NON_UNIQUE &gt;&gt;&gt; false
  TYPE &gt;&gt;&gt; 3
  ORDINAL_POSITION &gt;&gt;&gt; 1
  COLUMN_NAME &gt;&gt;&gt; email
  ASC_OR_DESC &gt;&gt;&gt; A
  FILTER_CONDITION &gt;&gt;&gt; null
  TABLE_SCHEM &gt;&gt;&gt; dbo
  TABLE_CAT &gt;&gt;&gt; tempdb
  =====================================================
  </code></pre>
  
  <p>I've read through the following documentation link
  <a href="https://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getIndexInfo(java.lang.String,%20java.lang.String,%20java.lang.String,%20boolean,%20boolean)" rel="nofollow noreferrer">[JDBC doc]</a></p>
  
  <p>Also, read through these stackoverflow questions on the same topic</p>
  
  <p><a href="https://stackoverflow.com/questions/16358383/show-column-names-of-multiple-colum-index-via-jdbc">[Question 1]</a> 
  <a href="https://stackoverflow.com/questions/52059015/getindexinfo-returning-wrong-unique-columns-for-composite-primary-keys">[Question 2]</a>
  <a href="https://stackoverflow.com/questions/21001561/retrieve-all-indexes-for-a-given-table-with-jdbc">[Question 3]</a></p>
  
  <p>What should I do to get this done via JDBC? One option I have is to have DB specific queries to get the list of indices and run them through JDBC (but I don't want to go that route)</p>