5

I have several servers which use the Microsoft SQL Server JDBC driver. The files are all named sqljdbc4.jar. I need to know what version of the driver each one contains. Time stamps and file sizes are not helpful, as I need to extract the driver version number. I need to be able to run this on the command line.

I have seen for DB2 you can run this command and get the version:

java -cp ./db2jcc.jar com.ibm.db2.jcc.DB2Jcc -version

What is the equivalent, if any, for Microsoft SQL Server?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Mark Filley
  • 163
  • 2
  • 2
  • 8

4 Answers4

5

It seems that there is no dedicated CLI to print out the driver version but you can ask the MS 4.x driver for its version:

import java.sql.Driver;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
...
Driver driver = new SQLServerDriver();
driver.getMajorVersion();  // -> 4
driver.getMinorVersion();  // -> 0, 1, 2, ...

You could build a simple commandline wrapper to print out this information.

wero
  • 32,544
  • 3
  • 59
  • 84
1

IBM provides specific tooling in its JAR to provide for the behavior you describe. It is not a general feature of JDBC drivers or of JAR files.

Microsoft documents the available mechanisms for determining the driver version on MSDN. They offer two alternatives:

  • extract the information from an [SQLServer]DatabaseMetaData object obtained via the driver (i.e. by a Java program); the most appropriate method for your use appears to be getDriverVersion(). Or
  • extract the information from the readme.txt file provided in the distribution.

Unless readme.txt is packaged in the JAR (possible, but unlikely), the former approach is the only one that will work with the JAR file alone. It shouldn't be too hard to write a Java program and maybe a wrapper script to apply this approach to the task, but it doesn't look anywhere near as simple as for the DB2 driver.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

If you have access to a Java decompiler, you can open the SQLJDBC JAR with the decompiler and check this decompiled class: com\microsoft\sqlserver\jdbc\SQLJdbcVersion.class It shows version info like the following:

final class SQLJdbcVersion
{
   static final int major = 4;
   static final int minor = 0;
   static final int MMDD = 2206;
   static final int revision = 100;
}

I do not know if that class is available in all versions of the SQLJDBC JAR, but I have found it in the 3.0, 4.0, and 4.2 JARs. GitHub (link below) also shows it present in versions 6 thru 8. All versions appear to include the "major" and "minor" variables; the other variables which are included differ depending on the version.

To fetch that data from the command line, perhaps you could create a script to invoke the decompiler, then extract the major and minor values from the decompiled file, and delete the decompiled file(s).

The SQL Server JDBC driver is open-source, according to this page: https://learn.microsoft.com/en-us/sql/connect/jdbc/frequently-asked-questions-faq-for-jdbc-driver?view=sql-server-ver15

The JDBC Driver is open-source and the source code can be found on GitHub.

Therefore, there should be no legality issues in decompiling the JARs.

The current GitHub "dev" branch version of the code shows these values:

final class SQLJdbcVersion {
   static final int major = 8;
   static final int minor = 4;
   static final int patch = 0;
   static final int build = 0;
   /*
    * Used to load mssql-jdbc_auth DLL.
    * 1. Set to "-preview" for preview release.
    * 2. Set to "" (empty String) for official release.
    */
   static final String releaseExt = "";
}
Djinn
  • 102
  • 1
  • 1
  • 9
0

I suggest using this one liner for getting the internal MSSQL driver version:

$ echo "System.out.println(new com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData(null).getDriverVersion())" | /usr/java/jdk-11.0.2/bin/jshell -q --class-path mssql-jdbc-7.0.0.jre10.jar

jshell> System.out.println(new com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData(null).getDriverVersion())
7.0.0.0

jshell command is available starting from JDK9. Alternatively you can also use jrunscript on JDK8

$ jrunscript -cp mssql-jdbc-7.0.0.jre10.jar -e "java.lang.System.out.println(new com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData(null).getDriverVersion())"

Warning: Nashorn engine is planned to be removed from a future JDK release
7.0.0.0
Rod Rob
  • 1,026
  • 11
  • 9