javadatabasemetadataddlutils

Easiest way to obtain database metadata in Java?


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?


Solution

  • 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.
    }