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?
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”.
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.
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.
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With