The DatabaseMetaData interface is used to get information about the database and its features. It helps developers understand details like database name, version, driver, and tables.
In this chapter, you will learn about the DatabaseMetaData interface, its methods, and how to get database information using Java.
The DatabaseMetaData interface provides methods to get metadata of a database. This includes details like database product name, version, driver name, and information about tables and views.
It is useful when you want to explore or analyze the structure and properties of a database.
To use DatabaseMetaData in a program, we need to import it from the java.sql package, which contains all the required classes for database connectivity. In most cases, we import the entire package (java.sql.*) to easily access all JDBC components.
Here is the import statement to use DatabaseMetaData Interface:
Or we can use:
The following table lists important methods of the DatabaseMetaData interface.
| Method | Description |
|---|---|
| public String getDriverName() | Returns the name of the JDBC driver. |
| public String getDriverVersion() | Returns the version of the JDBC driver. |
| public String getUserName() | Returns the username of the database. |
| public String getDatabaseProductName() | Returns the name of the database product. |
| public String getDatabaseProductVersion() | Returns the version of the database. |
| public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) | Returns information about tables in the database. |
The getMetaData() method of the Connection interface is used to get the DatabaseMetaData object.
Here is the syntax to get the object of DatabaseMetaData:
The following code snippet demonstrates how to get database metadata.
Output:Driver Name: Oracle JDBC Driver
Driver Version: 10.2.0.1.0XE
Database Product Name: Oracle
Database Product Version: Oracle Database 10g Express Edition
Release 10.2.0.1.0 -Production
Explanation:
This example shows how to use DatabaseMetaData to get details about the database, such as driver name, version, username, and database information.
This example demonstrates how to retrieve and display all table names from the database using the DatabaseMetaData interface.
This example shows how to retrieve and display all view names from the database using the DatabaseMetaData interface.
We request you to subscribe our newsletter for upcoming updates.