How to Extract Database Metadata using JDBC?
Last Updated :
04 Apr, 2024
In JDBC, for extracting Metadata from the database we have one interface which is DatabaseMetaData. By using this interface, we can extract the metadata from the Database. We have a connection object It has getMetaData() method. After this, define the DatabaseMetaData and create an object for this. Then assign the result of the con.getMetaData() method. After this, by using the DatabaseMetaData object, we can extract the database metadata.
In this article, we will learn how to extract Database Metadata using JDBC.
DatabaseMetaData Interface
DatabaseMetaData is an interface in Java Programming, and it is part of JDBC also. It provides methods for getting metadata information about the database. It provides metadata about the database like its structure, capabilities, and other properties of the database. The metadata refers to data about the data of database with the help of JDBC. By using DatabaseMetaData, we can perform different tasks.
- Retrieving information about database products like database name, version of the database, driver version of the database, and other properties.
- And we can get information about tables, columns, primary keys, foreign keys, and other things.
- And it can determine SQL syntax supported by the database.
- Also, DatabaseMetaData provides information about database objects.
We can obtain metadata from the connection object:
DatabaseMetaData metaData = con.getMetaData();
Programs to Extract Database Metadata using JDBC
In this example, first we connect the database by using some database configuration properties like local host name, database name, username, password, database port number and other properties. After that, we have created object for DatabaseMetaDase class, for this object, we assign the result of con.getMetaData(). After that by using that object, we extract the below database information.
- Database Product Name
- Database Product Version
- Driver Name
- Driver Version
- Database Connection Information
- Database User name Information
- Database URL information
- Database Time Date functions information
1. Database Product Name
In this example, we wrote the logic to get Database Product Name by using DatabaseMetaDase object. This object provides a method for accessing Database product name. The method is getDatabaseProductName(). This method is available in DatabaseMetaDase.
Implementation:
Java
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
In this example, 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.
Java
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
In this example, we wrote logic for getting Database driver name which used in JDBC. For getting this information from database we need to use DatabaseMetaDase interface which have related methods. This interface provides getDriverName() method. This method accesses the information about Database Driver Name. After that it prints that Information.
Java
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
In this example, we wrote logic for getting Database driver version used in JDBC. For getting this information from database we need use DatabaseMetaDase interface which have related methods. This interface provides getDriverVersion() method. This method provides the information about Database Driver version. Finally, it prints that Information.
Java
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
In this piece of code, we wrote logic for getting formation about Database Connection. This is possible by using DatabaseMetaDase interface. This means that the interface provides related methods. By using that method, we can be able to fetch the Database Connection Information from Database by using getConnection(). This method retrieves information like JDBC driver name and Its connection object.
Java
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
Basically, Database users are used for different purpose and every user have different access criteria in Database management. In this example, we are getting the information about my Database default user information. We can create any number users in our database. For getting information about Database Username information, we used getUserName() method from DatabaseMetaDase information. We have default user that is root user.
Java
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
In this example, we are trying get the information about Database URL information by using DatabaseMetaDase interface. This interface has methods for this purpose by using that method, we able access the Database URL information. For this, we have used getURL() from Database URL information interface. And this method gives information like database name, host name, database name, and mysql port number.
Java
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
In this example, we gather information about database Time Date functions information. This means that the Java logic can provide Time and Data functions related information means Functions names. By using getDateTimeFunctions(). It is possible from DatabaseMetaDase Interface in Java. This interface provides lot of method to extract Database Metadata using JDBC.
Java
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.
Similar Reads
How to Export MySQL Database using Command Line?
Exporting MySQL databases using the command line utility in Windows is a knowledge every Database Administrator and developer should possess. The mysqldump utility is an easy-to-use program that can back up databases, replicate or transfer data from one server to another and migrate databases. In th
4 min read
How to connect MySQL database using Scala?
MySQL database connectivity using ScalaIntroduction:Since Scala is interoperable with Java, we can directly work with databases using JDBC. JDBC - Java DataBase Connectivity is a Java API that allows Java code or programs to interact with the database.Scala code blocks or programs similarly use thes
3 min read
How to Insert Records to a Table using JDBC Connection?
Before inserting contents in a table we need to connect our java application to our database. Java has its own API which JDBC API which uses JDBC drivers for database connections. Before JDBC, ODBC API was used but it was written in C which means it was platform-dependent. JDBC API provides the appl
4 min read
How to Export Database Schema Without Data in SQL?
Database Schema specifies the structure of a database with its components like tables, columns, and indexes. Exporting data in SQL is an essential task in database management used to perform functions like data backup, recovery, migration, data analysis, performance optimization, compliance, auditin
4 min read
How to Export PostgreSQL Database Without Data Using SQL?
When we are working with the PostgreSQL database, there are multiple times we need to export the database structure. This approach is useful when we create a skeleton database or migrate the schema changes for different environments or systems. In this article, we will explore the process of exporti
3 min read
How to Export Database and Table Schemas in SQLite?
Exporting database schemas in SQLite is an important task for database management, enabling functions like data backup, recovery, migration, and auditing. In this article, We will go through the process of exporting database and table schemas in SQLite by understanding various examples to manage SQL
4 min read
Describing Databases with MetaData - SQLAlchemy
In this article, we are going to see how to describe Databases with MetaData using SQLAlchemy in Python. Database Metadata describes the structure of the database in terms of Python data structures. The database usually consists of Tables and Columns. The Database Metadata serves us in generating SQ
6 min read
How to pre populate database in Android using SQLite Database
Introduction : Often, there is a need to initiate an Android app with an already existing database. This is called prepopulating a database. In this article, we will see how to pre-populate database in Android using SQLite Database. The database used in this example can be downloaded as Demo Databas
7 min read
Spring Data JDBC Extensions
In this article, We will explore the realm of Spring Data JDBC extensions. Discover how they enhance the functionalities of the Spring Data JDBC module. These extensions offer a range of tools that enable developers to handle database interactions, with skill-making tasks, like simplifying queries e
3 min read
How to Call Stored Functions and Stored Procedures using JDBC?
JDBC or Java Database Connectivity is a Java API to connect and execute the query with the database. It is a specification from Sun microsystems that provides a standard abstraction(API or Protocol) for java applications to communicate with various databases. It provides the language with java datab
5 min read