Easiest way to obtain database metadata in Java?
Asked Answered
R

2

9

I'm familiar with the java.sql.DatabaseMetaData interface, but I find it quite clunky to use. For example, in order to find out the table names, you have to call getTables and loop through the returned ResultSet, using well-known literals as the column names.

Is there an easier way to obtain database metadata?

Redmon answered 12/6, 2009 at 4:33 Comment(0)
R
11

It's easily done using DdlUtils:

import javax.sql.DataSource;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformFactory;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.platform.hsqldb.HsqlDbPlatform;

public void readMetaData(final DataSource dataSource) {
  final Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);
  final Database database = platform.readModelFromDatabase("someName");
  // Inspect the database as required; has objects like Table/Column/etc.
}
Redmon answered 12/6, 2009 at 4:36 Comment(0)
U
6

Take a look at SchemaCrawler (free and open source), which is another API designed for this purpose. Some sample SchemaCrawler code:

    // Create the options
final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
// Set what details are required in the schema - this affects the
// time taken to crawl the schema
options.setSchemaInfoLevel(SchemaInfoLevel.standard());
options.setShowStoredProcedures(false);
// Sorting options
options.setAlphabeticalSortForTableColumns(true);

// Get the schema definition 
// (the database connection is managed outside of this code snippet)
final Database database = SchemaCrawlerUtility.getDatabase(connection, options);

for (final Catalog catalog: database.getCatalogs())
{
  for (final Schema schema: catalog.getSchemas())
  {
    System.out.println(schema);
    for (final Table table: schema.getTables())
    {
      System.out.print("o--> " + table);
      if (table instanceof View)
      {
        System.out.println(" (VIEW)");
      }
      else
      {
        System.out.println();
      }

      for (final Column column: table.getColumns())
      {
        System.out.println("     o--> " + column + " (" + column.getType()
                           + ")");
      }
    }
  }
}

http://schemacrawler.sourceforge.net/

Uriniferous answered 16/6, 2009 at 19:12 Comment(6)
Do you need to close the connection yourself or does the getDatabase() method do that for you?Redmon
@AndrewSwan - SchemaCrawler will not close the connection for you. You need to close it yourself.Uriniferous
In that case you might like to update your example so that it closes the connection in a finally block?Redmon
Andrew, I added a comment to indicate this. I believe that stackoverflow examples should be kept minimal to illustrate the answer, rather than be complete production code taking best practices into account.Uriniferous
@SualehFatehi Does the latest release of Schematic Crawler support Java 6? I have seen only Java 7 in FAQS. For Java 6 which release should I use? Please let me know.Atrium
@BRS - You will need to use version 9.6. SchemaCrawler moved over to Java 7 in version 10.1.Uriniferous

© 2022 - 2024 — McMap. All rights reserved.