jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes describing the table!!!... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "describing the table!!!..." Watch "describing the table!!!..." New topic

describing the table!!!...

bala chidambaram

Joined: Jan 19, 2004
Posts: 24
Hello friend,
Upto some extend i know jdbc. how to describe a table in jdbc and getting info(fieldname and datatype) about the table?
Thank u.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Check out the database Metadata interface for database info and the resultset metadata interface for table related info.

So much trouble in the world -- Bob Marley
bala chidambaram

Joined: Jan 19, 2004
Posts: 24
Thank u bello...
create table student
name varchar2(20),
rno number(2)
would u help me with this example?
Thank u.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33119

You would do a "select * from student" query to get a resultset. Then you would use the rs.getMetaData() method to navigate through the metadata referenced above.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: describing the table!!!...
jQuery in Action, 3rd edition