wood burning stoves*
The moose likes JDBC and the fly likes How to get Table Names from a Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to get Table Names from a Database" Watch "How to get Table Names from a Database" New topic
Author

How to get Table Names from a Database

MohanRaj Gurubatham
Greenhorn

Joined: Sep 05, 2001
Posts: 16
Hi,
Could any one help me with code to list all the tables in a connected database.
Thanks in advance.
MohanrajG
Ravi Verma
Ranch Hand

Joined: Aug 30, 2001
Posts: 42
Originally posted by MohanRaj Gurubatham:
Hi,
Could any one help me with code to list all the tables in a connected database.
Thanks in advance.
MohanrajG

Use this SQL STATEMEMT: select * from tab;

Ravi
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
This worked for me. You may need to tweak it a little bit to suit your needs.

Bosun
------------------------
DatabaseMetaData myMT = conn.getMetaData();
String[] myTables = {"TABLE"};
ResultSet tables = myMT.getTables(null,
null, "%", myTables);
String tableName = null;
while (tables.next())
{
tableName = tables.getString("TABLE_NAME");
System.out.println(tableName);
}


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
MohanRaj Gurubatham
Greenhorn

Joined: Sep 05, 2001
Posts: 16
Originally posted by Bosun Bello:
This worked for me. You may need to tweak it a little bit to suit your needs.

Bosun


Thank you verymuch Bosun. It is working for me also without chaning anything.
I was trying with the following code for MySQL
ResultSet rs1=stmt.executeQuery("show tables");
while(rs1.next())
{
System.out.println("Tables.....:"+rs1.getString(1));
}
But it is vendor specific where as the solutions which you gave is general. I really appriciate you.
With Regards
MohanRajG
Suji N
Ranch Hand

Joined: Sep 04, 2001
Posts: 35
Thankyou Very Much Bosun Bello
This code [tables] is working fine. i used that one.
I have a doubt.
How to display no of tables existed in Database[tablecount]
if possible pl send me a sample code
thanks inadvance
yours
Suji N
 
GeeCON Prague 2014
 
subject: How to get Table Names from a Database