Open In App

How to Retrieve Database Information using Database Meta Data in JDBC?

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

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 retrieve database Information Using Database MetaData in JDBC.

Retrieve Database Information Using Database MetaData in JDBC

This metadata provides valuable insights into the structure of the database which is used for various advantages like generation of dynamic queries and validations of the data and schema introspection. This access to the database metadata enhances the flexibility, efficiency, and maintainability of the Java applications that rely on the JDBC for database connectivity and management.

Prerequisites:

The following are the prerequisites to retrieve database information using DatabaseMetaData in JDBC

  1. Knowledge of Java programming
  2. Basic understanding of SQL
  3. Corresponding JDBC driver
  4. Eclipse IDE (Optional)

Note: Ensure that your database server is running and accessible from the Java applications.

Step-by-Step instructions to retrieve database information using DatabaseMetaData in JDBC

Step 1: Set up Eclipse Project

  1. Open Eclipse IDE and create one new Java project.
  2. After that create a new class in a Java project and name it as “DatabaseMetadata“.
  3. If you are not using Maven/Gradle dependency management, you must add the JDBC driver for your database connection.

Here is the path for the “Java Class” and “JDBC driver” file

File Structure

Path for class file and jar file

Step 2: Implement the code

Java
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DatabaseMetadataExample {
    public static void main(String[] args) {
        Connection connection = null;
        try {
            // Establish connection to the database
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/work", "root", "tiger");

            // Get DatabaseMetaData
            DatabaseMetaData metaData = connection.getMetaData();

            // Retrieve table names
            ResultSet resultSet = metaData.getTables(null, null, "%", new String[]{"TABLE"});
            System.out.println("Tables in the database:");
            while (resultSet.next()) {
                String tableName = resultSet.getString("TABLE_NAME");
                System.out.println(tableName);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // Close connection
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

Explanation of the above Program:

  1. In the above example, we are establish the connection to the MySQL database with the database URL, username and password. The connection is create with the “DriverManager.getConnection()” method.
  2. After establish the connection, it will obtain the “DatabaseMetaData” object which is named as “metaData“. This object is allowed access to the metadata about database.
  3. The above code retrieves the information about the tables in the MySQL database with the help of getTables() method of “DatabaseMetadata“.
  4. try-catch block is used to caught and handled the SQL Exceptions.
  5. If an exception occurred in the code, the stack trace is prints to aid in the debugging.
  6. In finally block the connection is closed with the “connection.close()” method.

Note: Make sure that you must be replace the database URL, username and password respectively.

Step 3: Run the Application

  1. After implementation of the code, you need to run the Java project.
  2. For run the class file, right click on Java project and then select Run As > Java Application.

The output will be shown in your console window of your Eclipse IDE as shown below:

Output:

Output Created

Output

The above output shows the list of tables which are present in the database.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads