posted 20 years ago
There are a couple of ways of doing this. One is to use the DatabaseMetaData and the other is to use the ResultSetMetaData. Both methods require a database connection, but the first method doesn't require you to run a query.
To use the DatabaseMetaData, here's an example (for Oracle):
The statement "rs = dbmd.getTables(null, "some-user", "%", xxx);" looks for
all tables in the "some-user" schema. If you want tables regardless of the schema, replace "some-user" with null. If you want to look at a specific table, put that table name in place of the "%" (the wildcard specification).
There is a lot of useful database information that can be pulled from the DatabaseMetaData.
If you go the second route as Jeanne has pointed out, I'd use the following query instead:
"select * from student where 1 = 2"
Since student (or any other table) might contain a large number of rows, there is no need to force the database to do a lot of work. If you give it an invalide condition it won't return any rows, but you'll still get the ResultSetMetaData, and can query for the column names and types.
I've tested the above approach using Oracle, Sybase and MySql and it works with each of these.
If you had a key field, such as "int studentId", you could use:
"select * from student where studentId = 0" and it would have the same affect.