How do I get JDBC driver information?

package org.kodejava.example.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

public class DriverInfo {
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost/kodejava";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";

    public static void main(String[] args) throws Exception {
        Connection connection = null;
        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

            DatabaseMetaData metadata = connection.getMetaData();
            String driverName = metadata.getDriverName();
            String driverVersion = metadata.getDriverVersion();
            int majorVersion = metadata.getDriverMajorVersion();
            int minorVersion = metadata.getDriverMinorVersion();

            System.out.println("driverName = " + driverName);
            System.out.println("driverVersion = " + driverVersion);
            System.out.println("majorVersion = " + majorVersion);
            System.out.println("minorVersion = " + minorVersion);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
}

The result of our program:

driverName = MySQL-AB JDBC Driver
driverVersion = mysql-connector-java-5.0.3 ( $Date: 2006-07-26 17:26:47 +0200 (Wed, 26 Jul 2006) $, $Revision: 5553 $ )
majorVersion = 5
minorVersion = 0

Wayan Saryada

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply