Get Database Minor Version using DatabaseMetaData in Java



In this article, we will learn how to use the getDatabaseMinorVersion() method of the DatabaseMetaData interface in Java to retrieve the minor version of the underlying database. This method helps check the version details of the connected database.

What is DatabaseMetaData?

The DatabaseMetaData is an interface that provides methods to retrieve metadata about a database, such as the database product name, version, driver name, the total number of tables, views, and more.
To get the DatabaseMetaData object, use the getMetaData() method of the Connection interface.
Syntax:
public DatabaseMetaData getMetaData() throws SQLException;

Commonly Used Methods of DatabaseMetaData Interface

The following are the common methods of DatabaseMetaData interface ?

  • public String getDriverName() throws SQLException: Returns the name of the JDBC driver.
  • public String getDriverVersion() throws SQLException: Returns the version of the JDBC driver.
  • public String getUserName() throws SQLException: Returns the username of the connected database.
  • public String getDatabaseProductName() throws SQLException: Returns the product name of the database.
  • public String getDatabaseProductVersion() throws SQLException: Returns the version of the database product.

Steps to Retrieve Database Minor Version

The getDatabaseMinorVersion() method of the DatabaseMetaData interface returns the minor version of the underlying database in integer format.

To get the list of minor versions of the underlying database ?

  • Step 1: Register the MySQL Driver: 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.
DriverManager.registerDriver(new com.mysql.jdbc.Driver());

  • Step 2: Establish Database Connection: Get the connection object using the getConnection() method of the DriverManager class. Pass the URL of the database, the username, and the password of a user in the database, as String variables.
String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
  • Step 3: Retrieve the DatabaseMetaData Object: Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.
DatabaseMetaData metaData = con.getMetaData();

  • Step 4: Retrieve the Minor Version: get the minor version of the database using the getDatabaseMinorVersion() method of the DatabaseMetaData interface.
int version = metaData.getDatabaseMinorVersion();

System.out.println("Minor version of the underlying database: " + version);

Java Program to Get Database Minor Version

Following JDBC program establishes a connection with the MySQL database, retrieves and, displays the minor version of the underlying database.

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseMetaData_getDatabaseMinorVersion {
	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();
//Retrieving the minor version of the database
		int version = metaData.getDatabaseMinorVersion();
		System.out.println("minor version of the underlying database: "+version);
	}
}

Output

Connection established......
minor version of the underlying database: 7
Updated on: 2025-01-02T19:13:43+05:30

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements