Wednesday 1 October 2014

Get Driver details

java.sql.DatabaseMetaData interface provides below methods to get driver details.

int getDriverMajorVersion()
Return this JDBC driver's major version number.

int getDriverMinorVersion()
Return this JDBC driver's minor version number.

String getDriverName()
Return the name of this JDBC driver.

String getDriverVersion()
Returns the version number of this JDBC driver as a String.

/* Step 1: Import sql package */
import java.sql.*;

public class SampleApp {
    
    static Connection conn;
    static Statement stmt;
    
    /* Update username, password and driver details here */
    static Connection getConnection() throws ClassNotFoundException, SQLException{
         /* Step 2: Load Driver */
        System.out.println("Loading/Registering driver");  
       
        Class.forName("com.mysql.jdbc.Driver");
         
        /* Step 3: Open connection to database */
        System.out.println("Connecting to database");
        String url = "jdbc:mysql://localhost/world";
        String userName = "root";
        String pasword = "tiger";
        return DriverManager.getConnection(url, userName, pasword);
    }
    
    public static void main(String args[]) throws SQLException, ClassNotFoundException{      
       
        conn = getConnection();
        DatabaseMetaData dm = conn.getMetaData();
        
        System.out.println("Major Version: " +dm.getDriverMajorVersion());
        System.out.println("Minor Version: " +dm.getDriverMinorVersion());
        System.out.println("Driver Name: " +dm.getDriverName());
        System.out.println("Driver Version: " +dm.getDriverVersion());
        
        conn.close();
    }
}

Output
Loading/Registering driver
Connecting to database
Major Version: 5
Minor Version: 1
Driver Name: MySQL-AB JDBC Driver
Driver Version: mysql-connector-java-5.1.5 ( Revision: ${svn.Revision} )


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment