I have the code as follows:
DatabaseMetaData dmd = connection.getMetaData();
ResultSet rs = dmd.getPrimaryKeys(null, null, tableName);
while(rs.next()){
metadata interface implementation was implemented by driver vendors. It may not be supported by some driver and some db. Here is text from javadoc: Some DatabaseMetaData methods return lists of information in the form of ResultSet objects. Regular ResultSet methods, such as getString and getInt, can be used to retrieve the data from these ResultSet objects. If a given form of metadata is not available, an empty ResultSet will be returned.
table name is case sensitive in oracle
or try the below approach
DatabaseMetaData dm = conn.getMetaData( ); ResultSet rs = dm.getExportedKeys( "" , "" , "table1" ); while( rs.next( ) ) { String pkey = rs.getString("PKCOLUMN_NAME"); System.out.println("primary key = " + pkey); }
you can also use getCrossReference or getImportedKeys to retrieve primary key