Search This Blog

2017-01-04

MS SQL Server: Finding JDBC Version

Product: MS SQL Server
Version: 2008R2 - 2014

Every Microsoft JDBC JAR version bundled with 4 sqljdbc.jar files for 4 different versions of major Java JVM.  When you download the driver from Microsoft, it will include 4 JAR files, and you need to pick the proper JAR file according to your JVM version.

For example, following Microsoft JDBC 4.2 bundled with sqljdbc42.jar that needs JVM 1.8, sqljdbc41.jar that needs JVM 1.7, sqljdbc4.jar that needs JVM 1.6, and sqljdbc.jar that needs JVM 1.5.

https://msdn.microsoft.com/en-us/library/ms378422(v=sql.110).aspx

From this MSND, you can see that JDBC v4.2 has another versioning called JDBC Version Compliance for each file.

So when you received a file called sqljdbc.jar, it is hard to tell what version of Microsoft JDBC driver version it belongs to, but only able to tell it support JVM 1.5 or older.

Therefore, I developed following Java application that can display both the JDBC version, as well as JDBC Version Compliance

Sample Output

D:\temp\sqljdbc>C:\Language\jdk1.8.0_92_64\bin\java -cp sqljdbc42.jar;. JDBCVersion
JDBC Driver version: 4.2
Driver name: Microsoft JDBC Driver 6.0 for SQL Server
Driver version: 6.0.7728.100
Product name: Microsoft SQL Server
Product version: 11.00.5058

D:\temp\sqljdbc>C:\Language\jdk1.8.0_92_64\bin\java -cp sqljdbc41.jar;. JDBCVersion
JDBC Driver version: 4.1
Driver name: Microsoft JDBC Driver 6.0 for SQL Server
Driver version: 6.0.7728.100
Product name: Microsoft SQL Server
Product version: 11.00.5058

D:\temp\sqljdbc>C:\Language\jdk1.8.0_92_64\bin\java -cp sqljdbc4.jar;. JDBCVersion
JDBC Driver version: 4.0
Driver name: Microsoft JDBC Driver 4.0 for SQL Server
Driver version: 4.0.4621.201
Product name: Microsoft SQL Server
Product version: 11.00.5058

D:\temp\sqljdbc>java -cp C:\ClaimCenter7.0.7\lib\sqljdbc4.jar;. JDBCVersion
JDBC Driver version: 4.0
Driver name: Microsoft SQL Server JDBC Driver 3.0
Driver version: 3.0.1301.101
Product name: Microsoft SQL Server
Product version: 11.00.5058

D:\temp\sqljdbc>java -cp C:\ClaimCenter9.0.0\admin\lib\sqljdbc-4.1.jar;. JDBCVersion
JDBC Driver version: 4.0
Driver name: Microsoft JDBC Driver 4.1 for SQL Server
Driver version: 4.1.5605.100
Product name: Microsoft SQL Server
Product version: 11.00.5058

Source code

Filename: JDBCVersion.java
import java.sql.*;

class JDBCVersion
{
    public static void main(String[] args) {

        Connection conn = null;

        try {

            String dbURL = "jdbc:sqlserver://localhost\\SQLSERVER2012";
            String user = "sa";
            String pass = "sa";
            conn = DriverManager.getConnection(dbURL, user, pass);
            if (conn != null) {
                DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
System.out.println("JDBC Driver version: " + dm.getJDBCMajorVersion() + "." + dm.getJDBCMinorVersion());
                System.out.println("Driver name: " + dm.getDriverName());
                System.out.println("Driver version: " + dm.getDriverVersion());
                System.out.println("Product name: " + dm.getDatabaseProductName());
                System.out.println("Product version: " + dm.getDatabaseProductVersion());

            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
}


Compiling Instruction

C:\Language\jdk1.8.0_92_64\bin\javac JDBCVersion.java

This will create JDBCVersion.class in current directory

Execution Instruction

1. Download MS SQL Server JDBC driver to C:\temp
2. Extracts all the JAR files into C:\temp\sqljdbc.  In typical usage, you will want to copy the JAR file that you would like to find out its version
3. Run and specify the JDBC jar files that you want to verify the version

C:\Language\jdk1.8.0_92_64\bin\java -cp sqljdbc4.jar;. JDBCVersion

where you have changed the current directory to where JDBCVersion.class resides

No comments: