File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get Table Names from a Database

 
MohanRaj Gurubatham
Greenhorn
Posts: 16
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic