DatabaseMetaData is an interface in Java Programming, and it is part of JDBC also. DatabaseMetaData is an interface in JDBC used to obtain information about the database, including its structure, properties, and supported features.
- Retrieves details like database name, version, and driver information.
- Provides information about tables, columns, primary keys, and foreign keys.
- Helps determine supported SQL syntax and database capabilities.
We can obtain metadata from the connection object:
DatabaseMetaData metaData = con.getMetaData();
Programs to Extract Database Metadata using JDBC
In this example, first connect to the database using configuration details like host, database name, username, password, and port. Then, we create a DatabaseMetaData object using con.getMetaData() and use it to retrieve database information.
- Database product name and version
- Driver name and version
- Connection and user information
- Database URL details
- Supported date and time functions
1. Database Product Name
Retrieve the database product name using the DatabaseMetaData object. It provides the getDatabaseProductName() method to access this information.
Implementation:
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class RetrieveDataExample {
public static void main(String[] args) {
try {
// load MySQL JDBC driver class
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");
// Retrieve metadata about the database
DatabaseMetaData metaData = con.getMetaData();
// Print the name of the database product
System.out.println("\n\tDatabase Product Name: " + metaData.getDatabaseProductName());
// Close the database connection
con.close();
} catch (ClassNotFoundException | SQLException e)
{
// handle exceptions that will occur during the process
System.out.println("Exception is " + e.getMessage());
}
}
}
Output:
Below we can refer the output in console.

2. Database Product Version
By using DatabaseMetaDase we get the information about data. Here, we get the information like database product version by using getDatabaseProductVersion() from DatabaseMetaDase. We get the current version of database.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class RetrieveDataExample {
public static void main(String[] args) {
try {
// load MySQL JDBC driver class
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");
// Retrieve metadata about the database
DatabaseMetaData metaData = con.getMetaData();
// Print the version of the database product
System.out.println("\n\tDatabase Product Version: " + metaData.getDatabaseProductVersion());
// Close the database connection
con.close();
} catch (ClassNotFoundException | SQLException e)
{
// handle exceptions that will occur during the process
System.out.println("Exception is " + e.getMessage());
}
}
}
Output:
Below we can refer the output in console.

3. Driver Name
Get the database driver name using the DatabaseMetaData interface. It provides the getDriverName() method to retrieve and display the driver information.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class RetrieveDataExample {
public static void main(String[] args) {
try {
// load MySQL JDBC driver class
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");
// Retrieve metadata about the database
DatabaseMetaData metaData = con.getMetaData();
// Print the name of the JDBC driver being used
System.out.println("Driver Name: " + metaData.getDriverName());
// Close the database connection
con.close();
} catch (ClassNotFoundException | SQLException e)
{
// handle exceptions that will occur during the process
System.out.println("Exception is " + e.getMessage());
}
}
}
Output: Below we can refer the output in console.

4. Driver Version
Retrieve the database driver version using the DatabaseMetaData interface. It provides the getDriverVersion() method to get and display the driver version information.
Implementation
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class RetrieveDataExample {
public static void main(String[] args) {
try {
// load MySQL JDBC driver class
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");
// Retrieve metadata about the database
DatabaseMetaData metaData = con.getMetaData();
// Print the version of the JDBC driver being used
System.out.println("Driver Version: " + metaData.getDriverVersion());
// Close the database connection
con.close();
} catch (ClassNotFoundException | SQLException e)
{
// handle exceptions that will occur during the process
System.out.println("Exception is " + e.getMessage());
}
}
}
Output:
Below we can refer the output in console.

5. Database Connection Information
Retrieve database connection details using the DatabaseMetaData interface. The getConnection() method helps access the connection object and related information like the JDBC driver.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class RetrieveDataExample {
public static void main(String[] args) {
try {
// load MySQL JDBC driver class
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");
// Retrieve metadata about the database
DatabaseMetaData metaData = con.getMetaData();
// Print the connection information
System.out.println("Database Connection Information: " + metaData.getConnection());
// Close the database connection
con.close();
} catch (ClassNotFoundException | SQLException e)
{
// handle exceptions that will occur during the process
System.out.println("Exception is " + e.getMessage());
}
}
}
Output:
Below we can refer the output in console.

6. Database Username Information
Retrieve the database username using the DatabaseMetaData interface. The getUserName() method returns the name of the current database user.
Implementation
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class RetrieveDataExample {
public static void main(String[] args) {
try {
// load MySQL JDBC driver class
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");
// Retrieve metadata about the database
DatabaseMetaData metaData = con.getMetaData();
// Print the username of the current user accessing the database
System.out.println("Database User Name : " + metaData.getUserName());
// Close the database connection
con.close();
} catch (ClassNotFoundException | SQLException e)
{
// handle exceptions that will occur during the process
System.out.println("Exception is " + e.getMessage());
}
}
}
Output:
Below we can refer the output in console.

7. Database URL information
Retrieve the database URL using the DatabaseMetaData interface. The getURL() method provides details like host name, database name, and port number.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class RetrieveDataExample {
public static void main(String[] args) {
try {
// load MySQL JDBC driver class
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");
// Retrieve metadata about the database
DatabaseMetaData metaData = con.getMetaData();
// Print the URL of the database connection
System.out.println("Database URL Information : " + metaData.getURL());
// Close the database connection
con.close();
} catch (ClassNotFoundException | SQLException e)
{
// handle exceptions that will occur during the process
System.out.println("Exception is " + e.getMessage());
}
}
}
Output:
Below we can refer the output in console.

8. Database Time Date functions information
Retrieve supported time and date functions using the DatabaseMetaData interface. The getDateTimeFunctions() method returns the list of available functions.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class RetrieveDataExample {
public static void main(String[] args) {
try {
// load MySQL JDBC driver class
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password");
// Retrieve metadata about the database
DatabaseMetaData metaData = con.getMetaData();
// Print the time and date functions supported by the database
System.out.println("Database Time Date Functions : " + metaData.getTimeDateFunctions());
// Close the database connection
con.close();
} catch (ClassNotFoundException | SQLException e)
{
// handle exceptions that will occur during the process
System.out.println("Exception is " + e.getMessage());
}
}
}
Output:
Below we can refer the output in console.
