• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

describing the table!!!...

 
bala chidambaram
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the database Metadata interface for database info and the resultset metadata interface for table related info.
 
bala chidambaram
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bala,
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.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic