A library for binding results of methods defined in DatabaseMetaData.
See Maven Central for available versions.
<dependency>
<groupId>io.github.jinahya</groupId>
<artifactId>database-metadata-bind</artifactId>
</dependency>All 26 methods in DatabaseMetaData that return ResultSet are bound to type-safe Java classes.
try (var connection = dataSource.getConnection()) {
var context = Context.newInstance(connection.getMetaData());
// Get all catalogs
List<Catalog> catalogs = context.getCatalogs();
// Get all tables (null = don't filter)
List<Table> tables = context.getTables(null, null, "%", null);
// Get columns for a specific table
List<Column> columns = context.getColumns("my_catalog", "my_schema", "my_table", "%");
}// Tables have typed accessors
for (Table table : tables) {
String catalog = table.getTableCat(); // may be null
String schema = table.getTableSchem(); // may be null
String name = table.getTableName();
String type = table.getTableType(); // "TABLE", "VIEW", etc.
}
// Get primary keys for a table
List<PrimaryKey> pks = context.getPrimaryKeys(
table.getTableCat(),
table.getTableSchem(),
table.getTableName()
);
// Get foreign keys pointing to this table
List<ExportedKey> exportedKeys = context.getExportedKeys(
table.getTableCat(),
table.getTableSchem(),
table.getTableName()
);JDBC uses null to mean "not applicable" in results and "don't filter" in parameters. This aligns naturally:
// Get a table (catalog/schema may be null depending on database)
Table table = tables.get(0);
// Pass values directly — null means "don't filter by this"
List<Column> columns = context.getColumns(
table.getTableCat(), // null → don't filter by catalog
table.getTableSchem(), // null → don't filter by schema
table.getTableName(),
"%"
);A lot of classes/methods defined in this module need to be tested with various kinds of real databases.
<dependency>
...
<scope>test</scope>
</dependency>$ mvn \
-Pfailsafe \
-Dit.test=ExternalIT \
-Durl='<your-jdbc-url>' \
-Duser='<your-own-user>' \
-Dpassword='<your-own-password>' \
clean test-compile failsafe:integration-test