DEVFYI - Developer Resource - FYI

Is there a way to find the primary key(s) for an Access Database table? Sun's JDBC-ODBC driver does not implement the getPrimaryKeys() method for the DatabaseMetaData Objects.

JDBC Interview Questions and Answers


(Continued from previous question...)

Is there a way to find the primary key(s) for an Access Database table? Sun's JDBC-ODBC driver does not implement the getPrimaryKeys() method for the DatabaseMetaData Objects.

 
// Use meta.getIndexInfo() will 
//get you the PK index. Once
// you know the index, retrieve its column name

DatabaseMetaData meta = con.getMetaData();

String key_colname = null;

// get the primary key information
rset = meta.getIndexInfo(null,null, table_name, true,true);
while( rset.next())
{
  String idx = rset.getString(6);
  if( idx != null)
  {
    //Note: index "PrimaryKey" is Access DB specific
    //      other db server has diff. index syntax.
    if( idx.equalsIgnoreCase("PrimaryKey"))
    {
      key_colname = rset.getString(9);
      setPrimaryKey( key_colname );
    }
  }
}

(Continued on next question...)

Other Interview Questions