Database Metadata in Java (JDBC)
DatabaseMetaData is an interface in Java’s JDBC API that provides information about the database, such as its structure, features, tables, and supported capabilities.
It is used when you want to discover properties of the database at runtime (e.g., for generating reports, tools, or dynamic queries).
You can get the DatabaseMetaData
object using:
Connection conn = DriverManager.getConnection(...); DatabaseMetaData metaData = conn.getMetaData();
Commonly Used DatabaseMetaData
Methods
Below are the key methods, grouped with explanation:
1. General Database Information
Method | Description |
---|---|
getDatabaseProductName() | Returns the name of the database (e.g., MySQL, Oracle). |
getDatabaseProductVersion() | Returns the version of the database. |
getDriverName() | Returns the name of the JDBC driver. |
getDriverVersion() | Returns the version of the JDBC driver. |
getURL() | Returns the database connection URL. |
getUserName() | Returns the username used to connect. |
2. Tables and Columns
Method | Description |
---|---|
getTables(...) | Returns a list of tables in the database. |
getColumns(...) | Returns column details for a specific table (data type, name, size, etc.). |
getPrimaryKeys(...) | Returns the primary key column(s) of a table. |
getImportedKeys(...) | Returns foreign key constraints (references to other tables). |
getExportedKeys(...) | Returns keys that other tables reference from this table. |
3. Features and Capabilities
Method | Description |
---|---|
supportsTransactions() | Checks if the database supports transactions. |
supportsBatchUpdates() | Checks if batch updates are supported. |
supportsStoredProcedures() | Checks if stored procedures are supported. |
supportsResultSetType(int type) | Checks if a specific ResultSet type is supported. |
4. Schemas, Catalogs, and Limits
Method | Description |
---|---|
getSchemas() | Lists all schemas in the database. |
getCatalogs() | Lists all catalogs in the database. |
getMaxConnections() | Maximum number of simultaneous connections. |
getMaxTableNameLength() | Maximum length for table names. |
Example Code
import java.sql.*; public class MetaDataExample { public static void main(String[] args) throws Exception { Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password"); DatabaseMetaData meta = con.getMetaData(); System.out.println("DB Name: " + meta.getDatabaseProductName()); System.out.println("DB Version: " + meta.getDatabaseProductVersion()); System.out.println("Driver: " + meta.getDriverName()); System.out.println("User: " + meta.getUserName()); ResultSet tables = meta.getTables(null, null, "%", new String[]{"TABLE"}); System.out.println("Tables:"); while (tables.next()) { System.out.println(" - " + tables.getString("TABLE_NAME")); } con.close(); } }
Summary
DatabaseMetaData
is used for querying information about the database structure and capabilities.- It is especially useful for dynamic applications, DB tools, and migration utilities.
- Most commonly used to list tables, get column details, and check DB support features.