Java DatabaseMetadata getCatalogs Method with Example



In this article, we'll look at how to retrieve and display the list of available catalogs (databases) in a MySQL instance using Java's JDBC (Java Database Connectivity) API. By running a Java program that connects to the MySQL server, we'll use the DatabaseMetaData class to fetch catalog names with the getCatalogs() method, showing each catalog's name on the console. This process demonstrates how to access and navigate metadata in MySQL using JDBC, offering insight into the structure of your database environment.

Steps to retrieve database catalogs 

The getCatalogs() method of the DatabaseMetaData interface returns the name of the underlying database in String format.

To get the list of catalogs of the database at an instance ?

  • Make sure your database is up and running.

  • Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.

  • Get the connection object using the getConnection() method of the DriverManager class. Pass the URL of the database and, the user name, and password of a user in the database, as String variables.

  • Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.

  • Finally, get the database catalog using the getCatalogs() method of the DatabaseMetaData interface.

Creating sample databases

Let us create 6 databases in MySQL using CREATE statements as shown below ?

Create database mydatabase;
Create database exampledatabase;
Create database sampledb;
Create database students;
Create database testdb;
Create database details;

Java program to retrieve database catalogs

Following the JDBC program establishes a connection with MySQL database, retrieves and, displays the list of catalogs in it at the instance ?

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetaData_getCatalogs {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Retrieving the meta data object
      DatabaseMetaData metaData = con.getMetaData();
      //Returns the name of the database
      ResultSet rs = metaData.getCatalogs();
      System.out.println("List of the catalogs in the database");
      while(rs.next()) {
         System.out.println(rs.getString(1));
      }
   }
}

Output

Connection established......
List of the catalogs in the database
information_schema
details
exampledatabase
mydatabase
mysql
performance_schema
sampledb
students
sys
testdb

Code explanation

This Java program connects to a MySQL database using JDBC, retrieves the list of available database catalogs, and prints them. First, we register the MySQL driver and establish a connection using DriverManager.getConnection(). Then, we use the getMetaData() method to obtain a DatabaseMetaData object, which provides metadata about the database. Using metaData.getCatalogs(), we retrieve and loop through a list of catalogs, printing each catalog name to the console. This displays all databases currently available in the MySQL instance.

Updated on: 2024-11-14T17:40:52+05:30

776 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements