Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 786
Andrew Swan Avatar asked Jun 12 '09 04:06

Andrew Swan


People also ask

How do I find the metadata of a database?

Tables Metadata All of this can be done by using the getTables() method of the DatabaseMetaData object. Here, the first two parameters are catalog and schema. The third parameter takes a pattern of table names. For instance, if we provide “CUST%”, this will include all the tables whose name starts with “CUST”.

What is metadata how is it obtained in Java?

The metadata means data about data i.e. we can get further information from the data. If you have to get metadata of a table like total number of column, column name, column type etc. , ResultSetMetaData interface is useful because it provides methods to get metadata from the ResultSet object.

How do I get metadata from ResultSet?

The getMetaData() method of ResultSet interface retrieves the ResultSetMetaData object of the current ResultSet. This method returns a ResultSetMetaData object which holds the description of this ResultSet object's columns.


1 Answers

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.
}
like image 97
Andrew Swan Avatar answered Nov 04 '22 12:11

Andrew Swan