Obtain the table's primary key column name SQL
Select * From user_cons_columns
Where constraint_name = (select constraint_name from user_constraints
Where table_name = 'bst _ favorite 'and constraint_type = 'P ');
Remember: The table name must be in uppercase.
Run the following command to check all the primary keys in the table. The primary key column is in the fourth column.
Obtain all column names of a table.
Select * From table_name where rownum = 1
I believe everyone knows what this SQL statement means.
You can use the first statement above to query the primary key column name through JDBC programming.
Another method is to query the table's primary key column name by using JDBC programming. The Code is as follows:
Databasemetadata dbmd = conn. getmetadata ();
Rs = dbmd. getprimarykeys (null, null, tablename. touppercase (); // You must convert the table name to uppercase to correctly retrieve the primary key.
While (Rs. Next ())...{
Columnname = Rs. getstring (4 );
System. Out. println (columnname );
}
You can use the second SQL statement to obtain all the column names of a table using JDBC. The Code is as follows:
String SQL = "select * From table_name where rownum = 1 ";
STM = conn. createstatement ();
Rs1_cmd.exe cutequery (SQL );
Rs. Next ();
Resultsetmetadata metadata = Rs. getmetadata ();
For (INT I = 1; I <= metadata. getcolumncount (); I ++ )...{
System. Out. println (metadata. getcolumnname (I ));}