Jens Schauder
Jens Schauder

Reputation: 81862

Is there a Java API for comparing Database Schemas

I'd like to compare if

in two database schemas are identical.

Is there anything like this available? Maybe from one of the database migration managing tools?

Upvotes: 4

Views: 9352

Answers (4)

Bozho
Bozho

Reputation: 597026

LiquiBase has database diff. But I don't know if there is an API, or just the tool.

Upvotes: 2

Sualeh Fatehi
Sualeh Fatehi

Reputation: 4784

SchemaCrawler provides a Java API that presents database metadata as plain-old Java objects. While there is no API to compare the database metadata objects, but SchemaCrawler produces text (text, JSON, CSV, HTML) output that is designed to be diff-ed, using standard diff tools.

Sualeh Fatehi, SchemaCrawler

Upvotes: 3

stacker
stacker

Reputation: 68942

I don't know a high level API for schema comparison I used DatabaseMetaData it's not to hard to find differences i.g to retieve all tables you can do something like this:

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();

The following methods are also important for your intention:

getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) 
getExportedKeys(String catalog, String schema, String table)
getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) 
getPrimaryKeys(String catalog, String schema, String table) 

Upvotes: 3

duffymo
duffymo

Reputation: 308743

JDBC is the only Java API that deals with databases.

You'd have to connect to both, get their respective DatabaseMetaData, and compare the two.

Upvotes: 1

Related Questions