Java DatabaseMetaData getDriverVersion() method with example


The getDriverVersion() method of the DatabaseMetaData interface returns the version of the JDBC driver used.

To get the version 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 the database and, user name, 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 version of the JDBC driver used, by invoking the getDriverVersion() method of the DatabaseMetaData interface .

Example

Following JDBC program establishes connection with MySQL database, retrieves and, displays the version of the JDBC driver used.

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

Output

Connection established......
Version of the JDBC driver used: mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} )

Rishi Raj
Rishi Raj

I am a coder

Updated on: 30-Jul-2019

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements