how can i get the column names in a table

Discussions

General J2EE: how can i get the column names in a table

  1. how can i get the column names in a table (6 messages)

    hello
    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!
  2. how can i get the column names in a table[ Go to top ]

    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).

    thank you!
  3. how can i get the column names in a table[ Go to top ]

    Have you tried using DatabaseMetaData.getColumns(...) ?

    David
  4. how can i get the column names in a table[ Go to top ]

    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.
  5. 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.
  6. how can i get the column names in a table[ Go to top ]

    Hi ,
    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.

    Cheers !!!
    Aashish Kaushik
  7. This could help you out[ Go to top ]

    You could try this: SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
     
    FROM INFORMATION_SCHEMA.COLUMNS
     
    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.