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 Execute a SQL Query Using JDBC?
Java programming provides a lot of packages for solving problems in our case we need to execute SQL queries by using JDBC. We can execute a SQL query in two different approaches by using PreparedStatement and Statement. These two interfaces are available in java.sql package. When comparing both of t
4 min read
How to Use Flyway Database Migration Tool with JDBC?
Flyway is a powerful database migration tool. It simplifies and automates the process of managing database schema changes. Flyway follows the simple principle of versioning the database schema using SQL scripts. These scripts are organized in a specific folder structure and executed in a sequential
2 min read
How to Add a New Column to a Table Using JDBC API?
Java has its own API which JDBC API which uses JDBC drivers for database connections. JDBC API provides the applications-to-JDBC connection and JDBC driver provides a manager-to-driver connection. Following are the 5 important steps to connect the java application to our database using JDBC. Registe
2 min read
How to Retrieve Database Information using Database Meta Data in JDBC?
JDBC (Java Database Connectivity) is the standard Java API for connecting and interacting with relational databases. It allowed to the Java applications to execute the SQL queries, retrieve the results, and perform the operations of the database in programmatically. In this article, we will learn to
3 min read
How to add Image to MySql database using Servlet and JDBC
Structured Query Language or SQL is a standard Database language which is used to create, maintain and retrieve the data from relational databases like MySQL, Oracle, SQL Server, PostGre, etc. In this article, we will understand how to add an image to the MYSQL database using servlet. MYSQL is a rel
6 min read
How to Create, Edit & Alter Tables Using Java?
In Java, JDBC can be used to create, edit, and alter the tables. JDBC can be defined as Java Database Connectivity. It is an API that allows Java programs to interact with the databases. JDBC can implement Java programs to execute SQL queries and manipulate databases through a standard interface. In
5 min read
How to Execute a SQL Script File using JDBC?
JDBC stands for Java Database Connectivity. It is an API (Application Programming Interface) that allows developers to connect their Java code to any SQL database instance. The connection gives the power to developers to execute any SQL queries, be it creating, updating, or data retrieving queries,
6 min read
How to Get the Datatype of a Column of a Table using JDBC?
Java supports many databases and for each database, we need to have their respective jar files to be placed in the build path to proceed for JDBC connectivity. MySQL: mysql-connector-java-8.0.22 or similar mysql connectors with different versions. In this article, we are using mysql-connector-java-8
5 min read
Java Program to Insert Details in a Table using JDBC
Java Database Connectivity is basically a standard API(application interface) between the java programming language and various databases like Oracle, SQL, Postgres, SQL, etc. It connects the front end(for interacting with the users ) with the backend( for storing data). Algorithm: Search/ Insert/ D
4 min read
How to Update Contents of a Table using JDBC Connection?
JDBC (Java Database Connectivity) is basically a standard API(application interface) between the java programming language and various databases like Oracle, SQL, PostgreSQL, etc. It connects the front end(for interacting with the users) with the backend( for storing data). Steps to Update the conte
3 min read