• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

How to get Table Names from a Database

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic