Java DatabaseMetaData getDriverName Method with Example



In this article, we will learn about the DatabaseMetaData getDriverName() method in Java. The DatabaseMetaData interface provides useful methods to obtain details about the database and the JDBC driver. One such method is getDriverName(), which returns the name of the JDBC driver being used.

What is getDriverName() in Java?

The getDriverName() method belongs to the DatabaseMetaData interface in Java's JDBC API. It retrieves the name of the JDBC driver currently connected to the database.

Syntax

String driver_name = metaData.getDriverName();

The method returns the following values ?

  • Returns a String representing the name of the JDBC driver.
  • Throws SQLException if a database access error occurs.

Retrieving the Names of the JDBC Driver Used

The following program demonstrates how to use the getDriverName() method. It connects to a MySQL database and retrieves information about the names of the JDBC drivers used in the program.

To get the name of the JDBC driver used to connect with the database ?

  • 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 to the current connection using the getMetaData() method of the Connection interface.
  • Finally get the name of the JDBC driver used, by invoking the getDriverName() method of the DatabaseMetaData interface.

Registering the Driver: Select the required database and register the Driver class of the particular database using the registerDriver() method of the DriverManager class ?

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

Get connection: Create a connection object by passing the URL of the database, username, and password of a user in the database (in string format) as parameters to the getConnection() method of the DriverManager class ?

DriverManager.getConnection(url, user, password);

Retrieving Metadata: Retrieves the DatabaseMetaData object ?

DatabaseMetaData metaData = con.getMetaData();

Fetching Names: Fetches metadata about the names of JDBC drivers and stores it in the string driver_name ?

String driver_name = metaData.getDriverName();

Example

Following JDBC program establishes a connection with MySQL database, retrieves and, displays the name of the JDBC driver used ?

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseMetaData_getDriverName {
   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 name of the JDBC driver
      String driver_name = metaData.getDriverName();
      //Retrieves this JDBC driver's major version number
      System.out.println("Name of the JDBC driver used: "+driver_name);
   }
}

Output

Connection established......
Name of the JDBC driver used: MySQL-AB JDBC Driver

Time Complexity: O(1), constant time operations for metadata retrieval.
Space Complexity: O(1), only a few variables are used, and no additional space is needed.

Conclusion

The DatabaseMetaData.getDriverName() method in Java is a valuable tool for retrieving JDBC driver details. It provides insights into the database connection and helps developers manage database compatibility efficiently.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-02-27T19:27:42+05:30

372 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements