- Posted by: u u
- Posted on: November 19 2003 23:26 EST
in my projects,i need to get the column names of a unknown table,i have tried the ResultSetMetaData and DatabaseMetaData objects,it seems that both of them don't work,who can give me a code snippet?
such as retrieve the column names of the emp table(the oracle sample database---scott/tiger).
thank you very much!
- how can i get the column names in a table by u u on November 20 2003 00:17 EST
- how can i get the column names in a table by Aashish Kaushik on November 21 2003 05:39 EST
- This could help you out by Noe Samuhel on June 17 2011 06:46 EDT
sorry,i know that i can get it by the ResultSetMetaData.getColumnCount(int col) method,but i must execute a query first,because i know nothing about the table,the only choice is "select * from myTable",then navigate the resultset.but maybe this table have thousands of records,i only want to get the COLUMN NAME,not the record,so this method is not effective,who can give me a more effective code snippet?such as retrieve the column names of the emp table(in oracle sample database-----scott/tiger).
Have you tried using DatabaseMetaData.getColumns(...) ?
Use a dummy query to pull results out of your table:
select * from myTable where 1=2
Since 1=2 is always false, this query will return no records, but the JDBC driver will then be able to retrieve metadata about your table.
The following query really works to get the column names of a table.i think u may use this for dynamic tables it seems. use this query:
select column_name from user_tab_columns where table_name='enter the table name in caps';
example: select column_name from user_tab_columns where table_name='EMP';
Try this one and send the reply about the result.i think u can get what u need by this query.
I have gone through some of the responses posted and found some of them to be Dumbest ever.
First you must get your Basics right.
<1>Though ResultSetMetaData does provide a method by the name of getColumnNames() but you cannot guarrante that the names of the column returned are the exact replica of the Table column name
(Consider a case where queries have Alias names defined for Column names,In that case this method will return the name of thos aliases defined)
I hope this helps.
You could try this: SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
WHERE column_name LIKE '%watcher%'
[AND table_schema = 'database'], I believe it could help you find a specific column in an unknown table in a database. Also, try using the J1939 protocol stack, because it might be useful in your work.