Add Microsoft SQL JDBC driver to Maven

Maven does not directly support some libraries, like Microsoft’s SQL Server JDBC. This tutorial will show you how to add an external dependency to your local Maven repository. It assumes you have already installed Maven.

Download the JDBC driver for Microsoft SQL Server

  1. Visit the MSDN site for SQL Server and download the latest version of the JDBC driver for your operating system.
  2. Unzip the package
  3. Open a command prompt and switch into the expanded directory where the jar file is located.
  4. Execute the following command. Be sure to modify the jar file name and version as necessary:
  5. mvn install:install-file -Dfile=sqljdbc4.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0
  6. You should see something similar to this:
  7. [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven Stub Project (No POM) 1
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ standalone-pom ---
    [INFO] Installing /Users/claude/installers/JDBC/sqljdbc_4.0/enu/sqljdbc4.jar to /Users/claude/.m2/repository/com/microsoft/sqlserver/sqljdbc4/4.0/sqljdbc4-4.0.jar
    [INFO] Installing /var/folders/c6/q1bdtq557kv54783p1g6cbsw0000gp/T/mvninstall1874482299687761721.pom to /Users/claude/.m2/repository/com/microsoft/sqlserver/sqljdbc4/4.0/sqljdbc4-4.0.pom
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.208s
    [INFO] Finished at: Tue Mar 13 14:07:31 EDT 2012
    [INFO] Final Memory: 3M/81M
    [INFO] ------------------------------------------------------------------------
    

    Modify your POM

    Include the new dependency by modifying your project’s pom.xml. Add the following dependency:

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
    </dependency>
    

    Save the pom.xml file and build the project to make sure no errors exist.

This entry was posted in Framework, How-To, Tutorials and tagged , , , , , on by .

About the author

Claude is an independent solutions architect at a renowned financial services firm. He also helps small businesses increase their productivity, improve back-office processes and reduce costs by promoting the use of new technologies.

7 thoughts on “Add Microsoft SQL JDBC driver to Maven

  1. Róbert

    Thanks a lot for the howto, but you did not check that stuff for the pom.xml before pasting it, right? groupId and artifactId are spelled with a capital i.

  2. Claude Post author

    You are welcome. And you are absolutely right, those properties were not in the correct case. They are correct now.

  3. Gurvinder

    My code was working fine with MySQL. I replace dependency for MSQL and made required change in rest of code for Connection URL etc.
    When I am trying to run my program, I am getting following error:

    Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
            at sun.security.util.SignatureFileVerifier.processImpl(Unknown Source)
            at sun.security.util.SignatureFileVerifier.process(Unknown Source)
            at java.util.jar.JarVerifier.processEntry(Unknown Source)
            at java.util.jar.JarVerifier.update(Unknown Source)
            at java.util.jar.JarFile.initializeVerifier(Unknown Source)
            at java.util.jar.JarFile.getInputStream(Unknown Source)
            at sun.misc.URLClassPath$JarLoader$2.getInputStream(Unknown Source)
            at sun.misc.Resource.cachedInputStream(Unknown Source)
            at sun.misc.Resource.getByteBuffer(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: org.feed.JAXBUnmarshaller. Program will exit.
    
  4. Claude Post author

    It doesn’t look like your exception relates to the JDBC change. org.feed.JAXBUnmarshaller could not be found.

  5. Mejnar

    I’m not sure this is the way to do it. This will add those artifacts to your local workstation’s repository. I would say only to use the method described here as your absolutely LAST resort.

    Maven is all about consistency. If there’s more than one developer working on the project he/she should get the same downloaded artifacts as you do.

    The correct, but somewhat cumbersome, solution is to upload such artifacts to your company-wide Maven repository where there’s already a place for that called “3rd part library repo”. Yes, this requires that you run such a company-wide Maven Repository Manager. There’s Archiva, Artifactory and Nexus to choose from.

  6. Claude Post author

    The ideal development environment, especially a corporate one with many developers, requires an internal repository for artifacts not available on the central repository.

    This example is a solution for those lone developers who need to add a dependency that is not open source and don’t have a corporate repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>