4

Is there a way to identify SQL Server version using JDBC implementations? I specifically want to identify between 2008 and 2012.

Motive : 2008 doesnt support OFFSET but 2012 does. So, I need to add conditional code in my application to modify the SQL query appropriately.

MAmateur
  • 139
  • 2
  • 9
  • I believe I found the answer here http://stackoverflow.com/questions/18753886/sql-server-file-names-vs-versions . -Thanks – MAmateur Oct 07 '14 at 10:04
  • `DatabaseMetaData.getDatabaseMajorVersion()` and `DatabaseMetaData.getDatabaseMinorVersion()` –  Oct 07 '14 at 10:27

1 Answers1

6
import java.sql.*;
/**
  * Microsoft SQL Server JDBC test program
  */
public class Test {
  public Test() throws Exception {
    // Get connection
    DriverManager.registerDriver(new
    com.microsoft.jdbc.sqlserver.SQLServerDriver());
    Connection connection = DriverManager.getConnection(
    "jdbc:microsoft:sqlserver://<Host>:1433",<"UID>","<PWD>");
    if (connection != null) {
     System.out.println();
      System.out.println("Successfully connected");
      System.out.println();
      // Meta data
      DatabaseMetaData meta = connection.getMetaData();
      System.out.println("\nDriver Information");
      System.out.println("Driver Name: "
       + meta.getDriverName());
      System.out.println("Driver Version: "
       + meta.getDriverVersion());
      System.out.println("\nDatabase Information ");
      System.out.println("Database Name: "
       + meta.getDatabaseProductName());
      System.out.println("Database Version: "+
      meta.getDatabaseProductVersion());
    }
 } // Test
 public static void main (String args[]) throws Exception {
  Test test = new Test();
 }
}

Compile it

Compile the Java Source: Test.java (all in one line):

$ javac -classpath ".;./lib/mssqlserver.jar;
  ./lib/msbase.jar;./lib/msutil.jar" Test.java

Be aware that you need access to a javac program on your computer or media. If not, simply specify the full path ahead of javac. The above command is good for Java 2. If your are using for instance Java 1.1.8, add your JDK's classes.zip to the classpath. On Unix systems replace the the semicolons " ; " by colons " : " The forward slashes " / " are fine for both platforms, it's not a must to use backslashes " \ " on Windows.

Run it

Similar to the compilation you may run it like this (again all in one line):

$ java -classpath ".;./lib/mssqlserver.jar;
  ./lib/msbase.jar;./lib/msutil.jar" Test

The output looks something like this:

Successfully connected

Driver Information
  Driver Name: SQLServer
  Driver Version: 2.2.0022

Database Information
  Database Name: Microsoft SQL Server Database Version:
  Microsoft SQL Server 2000 8.00.194 (Intel X86)
     Aug  6 2000 00:57:48
     Copyright (c) 1988-2000 Microsoft Corporation
     Enterprise Edition on Windows NT 5.0
     (Build 2195: Service Pack 2)
Matt
  • 14,906
  • 27
  • 99
  • 149