Java DatabaseMetaData getJDBCMajorVersion() method with example



In this article, we will learn how to retrieve the major version of the JDBC driver using the getJDBCMajorVersion() method of the DatabaseMetaData interface in Java. This method is useful for checking the version of the JDBC driver in use.

getJDBCMajorVersion() method

The getJDBCMajorVersion() method of the DatabaseMetaData interface returns the major version of the JDBC driver in integer format.

To get a major version of the JDBC 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.

  • 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 major version of the JDBC driver using the getJDBCMajorVersion() method of the DatabaseMetaData interface.

Steps to retrieve JDBC driver version information

The following are the steps to retrieve JDBC driver version information ?

  • Step 1. Registering the JDBC Driver: The first step is to register the appropriate JDBC driver for your database. This is done using the registerDriver() method of the DriverManager class. This registers the MySQL JDBC driver to handle database connections.

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

  • Step 2. Establishing Database Connection: Establish a connection to the database using the getConnection() method of the DriverManager class. You need to provide the database URL, username, and password. This step connects to the MySQL database running on localhost with the specified credentials.
String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
  • Step 3. Retrieving Database MetaData: Once the connection is established, you can retrieve the DatabaseMetaData object using the getMetaData() method of the Connection interface. This object holds information about the database and its capabilities.
DatabaseMetaData metaData = con.getMetaData();

  • Step 4. Getting JDBC Major Version: Use the getJDBCMajorVersion() method of the DatabaseMetaData object to get the major version of the JDBC driver. This prints the major version of the JDBC driver, which is important for compatibility and functionality checks.
int version = metaData.getJDBCMajorVersion();
System.out.println("Major JDBC version number of the JDBC driver: "+version);

Example

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

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseMetaData_getJDBCMajorVersion {
	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 JDBC driver
		int version = metaData.getJDBCMajorVersion();
		System.out.println("Major JDBC version number of the JDBC driver: "+version);
	}
}

Output

Connection established......
Major JDBC version number of the JDBC driver: 3
Updated on: 2025-01-23T22:54:33+05:30

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements