I have the code as follows:
DatabaseMetaData dmd = connection.getMetaData();
ResultSet rs = dmd.getPrimaryKeys(null, null, tableName);
while(rs.next()){
primaryKey = rs.getString("COLUMN_NAME");
}
rs is not null while rs.next()
always return false
, anyone has idea about it? Thanks.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With