This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get Table Names from a Database

 
MohanRaj Gurubatham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
 
MohanRaj Gurubatham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic