Java – how to obtain the column name of the primary key through JDBC
•
Java
I have the following code:
DatabaseMetaData dmd = connection.getMetaData();
ResultSet rs = dmd.getPrimaryKeys(null,null,tableName);
while(rs.next()){
primaryKey = rs.getString("COLUMN_NAME");
}
RS is not empty, and rs.next() always returns false. Does anyone have an idea? thank you.
Solution
>The metadata interface is implemented by the driver vendor Some drivers and some DB may not support it
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 the primary key
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
