tags:

views:

83

answers:

3

Hi,

I'd like to retrieve all table names from a database schema, and, if possible, get all table starting with a specified prefix.

I tried using JDBC's connection.getMetaData().getTables() but it didn't work at all.

Connection jdbcConnection = DriverManager.getConnection("", "", "");
DatabaseMetaData dmd = jdbcConnection.getMetaData();
ResultSet tables = dmd.getTables(jdbcConnection.getCatalog(), null, "TAB_%", null);
for (int i = 0; i < tables.getMetaData().getColumnCount(); i++) {
   System.out.println("table = " + tables.getMetaData().getTableName(i));
}

Could someone help me on this ?

Thanks, and sorry for the noob question.

+3  A: 

You need to iterate over your ResultSet calling next().

This is an example from java2s.com:

DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
  System.out.println(rs.getString(3));
}

Column 3 is the TABLE_NAME (see documentation of getTables).

Peter Lang
Thanks ! you made my day :)
Maxime ARNSTAMM
A: 

I believe using sp_tables for mssql will work http://msdn.microsoft.com/en-us/library/ms186250.aspx or show tables; for mysql http://dev.mysql.com/doc/refman/5.0/en/show-tables.html

If I misundrerstand the question, please tell me and I will delete post.

RandyMorris
A: 

If you want to use a high-level API, that hides a lot of the JDBC complexity around database schema metadata, take a look at this article: http://www.devx.com/Java/Article/32443/1954

Sualeh Fatehi