Java Connection getMetaData Method with Example



Generally, Data about data is known as metadata. The DatabaseMetaData interface provides methods to get information about the database you have connected with like, database name, database driver version, maximum column length, etc...

The getMetaData() method of the Connection interface retrieves and returns the DatabaseMetaData object. This contains information about the database you have connected to. You can get information about the database such as the name of the database, version, driver name, user name, URL, etc? by invoking the methods of the DatabaseMetaData interface using the obtained object.

This method returns the DatabaseMetaData object which holds information about the underlying database.

Steps to Retrieve MySQL Database Metadata Using DatabaseMetaData

Below are the steps for retrieving MySQL database metadata using DatabaseMetaData ?

  • Import necessary packages
  • Register the MySQL driver
  • Establish a database connection
  • Create DatabaseMetaData object
  • Check for batch update support
  • Retrieve and print Metadata

Retrieving MySQL Database Metadata Using DatabaseMetaData

Below is the program in Java to retrieve MySQL database metadata using DatabaseMetaData?

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connection_getMetaData {
 public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String url = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(url, "root", "password");
System.out.println("Connection established......");
//Creating the DatabaseMetaData object
DatabaseMetaData dbMetadata = con.getMetaData();
//invoke the supportsBatchUpdates() method.
boolean bool = dbMetadata.supportsBatchUpdates();
if(bool) {
 System.out.println("Underlying database supports batch updates");
} else {
 System.out.println("Underlying database doesn't support batch updates");
}
//Retrieving the driver name
System.out.println("Driver name: "+dbMetadata.getDriverName());
//Retrieving the driver version
System.out.println("Database version: "+dbMetadata.getDriverVersion());
//Retrieving the user name
System.out.println("User name: "+dbMetadata.getUserName());
//Retrieving the URL
System.out.println("URL for this database: "+dbMetadata.getURL());
 }
}

Output

Connection established......
Underlying database supports batch updates
Driver name: MySQL-AB JDBC Driver
Database version: mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} )
User name: root@localhost
URL for this database: jdbc:mysql://localhost/mydatabase

Code explanation

To get the DatabaseMetaData object for the underlying database:

Register the driver using the registerDriver() method of the DriverManager class as ?

//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Get the connection using the getConnection() method of the DriverManager class as ?

//Getting the connection
String url = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(url, "root", "password");

Get the metadata object using the getMetaData() method as ?

DatabaseMetaData dbMetadata = con.getMetaData();

Following the JDBC program establishes a connection with the database and retrieves information about the underlying database such as the name of the database, driver name, URL, etc.

This Java program connects to a MySQL database and retrieves metadata using the DatabaseMetaData interface. It first registers the MySQL JDBC driver and establishes a connection using DriverManager. After the connection is established, it creates a DatabaseMetaData object to fetch database details. The program checks if the database supports batch updates and prints the result. It also retrieves and prints the driver name, driver version, user name, and database URL. This process is essential for understanding and managing database properties efficiently

Updated on: 2024-08-02T18:19:16+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements