views:

110

answers:

1

How can i programmatically get the names of all tables in a JavaDB database? Is there any specific SQL-statement over JDBC I can use for this or any built in function in JDBC?

I will use it for exporting the tables to XML, and would like to do it this way so I don't miss any tables from the database when exporting.

+4  A: 

With an open Connection con, do

DatabaseMetaData meta = con.getMetaData();
  ResultSet res = meta.getTables(null, null, null, 
     new String[] {"TABLE"});
  System.out.println("List of tables: "); 
  while (res.next()) {
     System.out.println(
        "   "+res.getString("TABLE_CAT") 
       + ", "+res.getString("TABLE_SCHEM")
       + ", "+res.getString("TABLE_NAME")
       + ", "+res.getString("TABLE_TYPE")
       + ", "+res.getString("REMARKS")); 
  }
  res.close();

EDIT: You question is about tables see http://java.sun.com/j2se/1.5.0/docs/api/java/sql/DatabaseMetaData.html#getTables(java.lang.String, java.lang.String, java.lang.String, java.lang.String[])

stacker
@Peter Lang, thanks fixed it in the same second ;-)
stacker