Is there a Java API for comparing database schemas

I would like to compare if

  • tables
  • including data types and length / precision.
  • indexes and their columns
  • limitations

in two schemas, the databases are identical.

Is there anything similar? Perhaps from one of the database migration management tools?

+4
source share
4 answers

I do not know the high level API for comparing schemas. I used DatabaseMetaData so that it is not difficult to find the differences ig for retrieving all the tables can be done 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 to your intent:

 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) 
+3
source

LiquiBase has a diff database . But I do not know if there is an API or just a tool.

+2
source

SchemaCrawler provides a Java API that presents database metadata as simple Java objects. Although there is no API for mapping database metadata objects, SchemaCrawler creates text (text, JSON, CSV, HTML) that is designed to be decomposed using standard comparison tools.

Sualeh Fatehi, SchemaCrawler

+2
source

JDBC is the only Java API that works with databases.

You will need to connect to both, get their corresponding DatabaseMetaData and compare them.

+1
source

All Articles