Open In App

How to Extract Database Metadata using JDBC?

Last Updated : 04 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Database Product Name Output

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.

Database Product Version Output

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.

 Driver Name Output

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.

Driver Version Output

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.

 Database Connection Information Output

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.

Database Username Information Output

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.

Database URL information Output

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.

Database Time Date functions information Output

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads