112

I am trying to add MS SQL driver dependency in my POM.xml file and the following is the dependency.

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

but I get this exception

Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0

I really don't understand the issue.

Jéf Bueno
  • 425
  • 1
  • 5
  • 23
Joe
  • 4,460
  • 19
  • 60
  • 106

11 Answers11

166

UPDATE

Microsoft now provide this artifact in maven central. See @nirmal's answer for further details: https://stackoverflow.com/a/41149866/1570834


ORIGINAL ANSWER

The issue is that Maven can't find this artifact in any of the configured maven repositories.

Unfortunately Microsoft doesn't make this artifact available via any maven repository. You need to download the jar from the Microsoft website, and then manually install it into your local maven repository.

You can do this with the following maven command:

mvn install:install-file -Dfile=sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0 -Dpackaging=jar

Then next time you run maven on your POM it will find the artifact.

DB5
  • 13,553
  • 7
  • 66
  • 71
  • thanks for the answer but i get this when i run the command `[ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4 :install-file (default-cli) on project standalone-pom: The specified file 'C:\Us ers\anthony\sqljdbc4.jar' not exists -> [Help 1]` – Joe Oct 23 '13 at 09:41
  • 3
    Make sure sqljdbc4.jar is places in directory you are running command in, otherwise provide full path explicitly. – Sergey Makarov Oct 23 '13 at 10:29
  • 2
    @Antony, as Sergey says, you need to either run the maven command (as given in my answer) from the same location where you downloaded the sqljdbc4.jar to or provide the full path to the file `-Dfile=C:\Users\anthony\Downloads\sqljdbc4.jar` for example. – DB5 Oct 23 '13 at 10:42
  • after installing sqljdbc4.jar as @DB5 said , It still gives me the same error when I run mvn package in my project , any thoughts on that? Where does it actually gets installed ? can I see a list of locally installed jars from mvn? – P-RAD Nov 20 '15 at 06:09
  • I got an error - [ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install-file (default-cli) on project standalone-pom: The artifact information is incomplete or not valid: [ERROR] [0] 'packaging' is missing. [ERROR] -> [Help 1] which was resolved by adding -Dpackaging=jar to the command – Sagar Mhatre Nov 23 '15 at 06:27
  • 5
    see [my answer](http://stackoverflow.com/a/41149866/306538) below, microsoft (as of nov 2016) added this jdbc driver to maven central – nirmal Dec 14 '16 at 18:45
  • Microsoft advices the last version which has jdbc42.jar not jdbc4.jar. If you want to keep on jdbc4.jar then you shoul go: https://www.microsoft.com/en-us/download/details.aspx?id=54629 – Ismail Yavuz May 23 '17 at 23:31
  • i find the sqljdbc4.jar here: https://osdn.net/frs/g_redir.php?m=tuna&f=id2d%2Fjdbc+drivers%2Fsqljdbc4.jar – chenchuk Nov 24 '18 at 21:13
84

Microsoft recently open sourced their jdbc driver.

You can now find the driver on maven central:

<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency>

or for java 7:

<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre7</version>
</dependency>
nirmal
  • 1,842
  • 18
  • 11
  • I've tried making this work for hours and found out that version 7.4.1 doesn't load a driver for me by version 7.0.0 does. Do you know why this might be the case? – Archmede Jan 20 '20 at 03:55
30

I had the similar problem and solved it by doing following.

  • Download sqljdbc4.jar from the Microsoft website to your local machine.
  • Right click on Project-->Import-->Maven-->Install or deploy an artifact to a Maven repository as shown below.

enter image description here

* Next-->Fill the following details

Artifact file: path of the jar you downloaded (Ex: E:\lib\sqljdbc4.jar in my case)
Group Id: com.microsoft.sqlserver
Artifact Id: sqljdbc4
Version: 4.0

enter image description here

  • Then Refresh/clean the project.

    Thank you!
programmer
  • 472
  • 4
  • 5
12

You can also create a project repository. It's useful if more developers are working on the same project, and the library must be included in the project.

  • First, create a repository structure in your project's lib directory, and then copy the library into it. The library must have following name-format: <artifactId>-<version>.jar

    <your_project_dir>/lib/com/microsoft/sqlserver/<artifactId>/<version>/

  • Create pom file next to the library file, and put following information into it:

    <?xml version="1.0" encoding="UTF-8"?>
     <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <modelVersion>4.2.0</modelVersion>
         <groupId>com.microsoft.sqlserver</groupId>
         <artifactId>sqljdbc4</artifactId>
         <version>4.2</version>
     </project>
     
  • At this point, you should have this directory structure:

    <your_project_dir>/lib/com/microsoft/sqlserver/sqljdbc4/4.2/sqljdbc4-4.2.jar <your_project_dir>/lib/com/microsoft/sqlserver/sqljdbc4/4.2/sqljdbc4-4.2.pom

  • Go to your project's pom file and add new repository:

    <repositories>
         <repository>
             <id>Project repository</id>
             <url>file://${basedir}/lib</url>
         </repository>
     </repositories>
     
  • Finally, add a dependency on the library:

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

Update 2017-03-04

It seems like the library can be obtained from publicly available repository. @see nirmal's and Jacek Grzelaczyk's answers for more details.

Update 2020-11-04

Currently Maven has a convenient target install which allow you to deploy an existing package into a project / file repository without the need of creating POM files manually. It will generate those files for you.

mvn install:install-file \
    -Dfile=sqljdbc4.jar \
    -DgroupId=com.microsoft.sqlserver \
    -DartifactId=sqljdbc4 \
    -Dversion=4.2 \
    -Dpackaging=jar \
    -DlocalRepositoryPath=${your_project_dir}/lib
Václav Kužel
  • 1,070
  • 13
  • 16
  • This should be the accepted answer, IMHO. Despite putting a binary into the git repository, it is a fire and forget solution. There is no need for further instruction on how to deploy the driver to each developer local repository (or searching StackOverflow for help). – lpacheco Jun 14 '16 at 19:31
  • @Ipacheco is correct, this should indeed be the accepted answer. – rjdamore Jul 15 '16 at 19:12
11

The above answer only adds the sqljdbc4.jar to the local repository. As a result, when creating the final project jar for distribution, sqljdbc4 will again be missing as was indicated in the comment by @Tony regarding runtime error.

Microsoft (and Oracle and other third party providers) restrict the distribution of their software as per the ENU/EULA. Therefore those software modules do not get added in Maven produced jars for distribution. There are hacks to get around it (such as providing the location of the 3rd party jar file at runtime), but as a developer you must be careful about violating the licensing.

A better approach for jdbc connectors/drivers is to use jTDS, which is compatible to most DBMS's, more reliable, faster (as per benchmarks), and distributed under GNU license. It will make your life much easier to use this than trying to pound the square peg into the round hole following any of the other techniques above.

Nelda.techspiress
  • 643
  • 12
  • 32
  • oh man jtds saved my day, configuring sqljdbc4 with gradle is some pain the a*s – norbertas.gaulia May 12 '15 at 16:46
  • Only thing is that jTDS doesn't support the same functionality as Microsoft's driver. For example, batch update. – lpacheco Jun 14 '16 at 19:27
  • 1
    it is unfortunate that jTDS does not support the datetime2 data type. seems my choices are to roll my own datetime2-to-java-date method, or go through a lot of hoops getting the microsoft driver to work. – jkerak Jul 28 '16 at 16:29
  • As of 2017, the driver has not been updated to JDBC4 and is pretty much dead, does not work with HikariCP and more modern conn pools. – Alfabravo Apr 17 '17 at 21:04
2

just add

 <dependency>
      <groupId>com.microsoft.sqlserver</groupId>
      <artifactId>sqljdbc4</artifactId>
      <version>4.0</version>
      <scope>runtime</scope>
 </dependency>
Jacek Grzelaczyk
  • 783
  • 2
  • 9
  • 21
1

For self-containing Maven project I usually installing all external jar dependencies into project's repository. For SQL Server JDBC driver you can do:

  • download JDBC driver from https://www.microsoft.com/en-us/download/confirmation.aspx?id=11774
  • create folder local-repo in your Maven project
  • temporary copy sqljdbc42.jar into local-repo folder
  • in local-repo folder run mvn deploy:deploy-file -Dfile=sqljdbc42.jar -DartifactId=sqljdbc42 -DgroupId=com.microsoft.sqlserver -DgeneratePom=true -Dpackaging=jar -Dversion=6.0.7507.100 -Durl=file://. to deploy JAR into local repository (stored together with your code in SCM)
  • sqljdbc42.jar and downloaded files can be deleted
  • modify your's pom.xml and add reference to project's local repository: xml <repositories> <repository> <id>parent-local-repository</id> <name>Parent Local repository</name> <layout>default</layout> <url>file://${basedir}/local-repo</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> Now you can run your project everywhere without any additional configurations or installations.
Ramunas
  • 11
  • 2
1

If you are having some issue when including dependency for 6.1.0.jre7 from @nirmals answer in https://stackoverflow.com/a/41149866/1570834, in your pom with commons-codec/ azure-keyvault I prefer going with this:

    <dependency>
       <groupId>com.microsoft.sqlserver</groupId>
       <artifactId>mssql-jdbc</artifactId>
       <version>6.2.2.jre7</version>                
    </dependency>
ROOP
  • 11
  • 3
0

It is not too hard. I have not read the license yet. However I have proven this works. You can copy sqljdbc4 jar file to a network share or local directory. Your build.gradle should look like this :

apply plugin: 'java'
//apply plugin: 'maven'
//apply plugin: 'enhance'

sourceCompatibility = 1.8
version = '1.0'

//library versions
def hibernateVersion='4.3.10.Final'
def microsoftSQLServerJDBCLibVersion='4.0'
def springVersion='2.5.6'

def log4jVersion='1.2.16'
def jbossejbapiVersion='3.0.0.GA'

repositories {
    mavenCentral()
    maven{url "file://Sharedir/releases"}
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile "org.hibernate:hibernate-core:$hibernateVersion"
    compile "com.microsoft.sqlserver:sqljdbc4:$microsoftSQLServerJDBCLibVersion"
}

task showMeCache << {
    configurations.compile.each { println it }
}

under the sharedir/releases directory, I have directory similar to maven structure which is \sharedir\releases\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar

good luck.

David Yen

0

You can use another driver

<dependency>
    <groupId>net.sourceforge.jtds</groupId>
    <artifactId>jtds</artifactId>
    <version>1.3.1</version>
</dependency>

and in xml

<bean id="idNameDb" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
        <property name="url" value="jdbc:jtds:sqlserver://[ip]:1433;DatabaseName=[name]" />
        <property name="username" value="user" />
        <property name="password" value="password" />
</bean>
Jacek Grzelaczyk
  • 783
  • 2
  • 9
  • 21
0

The com.microsoft.sqlserver package on Maven now only has version 6.0 as the lowest version JDBC. So you need try another groupId Maven which has JDBC version 4.0.

I recommend this; it works for me. I'm using SQL Server 2012 and Java 8.

<dependency>
    <groupId>net.sourceforge.jtds</groupId>
    <artifactId>jtds</artifactId>
    <version>1.3.1</version>
</dependency>

And a config properties file like:

jdbc.driverClassName = net.sourceforge.jtds.jdbc.Driver

jdbc.url = jdbc:jtds:sqlserver://localhost:1433;databasename=YourDB;encrypt=true;trustserverCertificate=true
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77