127

I am developing a portlet where I have Hibernate access to SQL Server database. I set up maven dependencies for it and try to find out SQL Server connector on the same way I know MySql has it.

Still my Google-search gives only Mysql if I search for SQL Server connector. What is the right maven dependency value?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mico
  • 12,730
  • 12
  • 59
  • 99

8 Answers8

213

Download the driver JAR from the link provided by Olaf and add it to your local Maven repository with;

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

Then add it to your project with;

<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>sqljdbc4</artifactId>
  <version>4.0</version>
</dependency>
Renaud
  • 16,073
  • 6
  • 81
  • 79
Stu.C
  • 2,298
  • 1
  • 15
  • 8
  • 13
    The release notes for the version linked from Olaf's answer state that it's "Microsoft JDBC Driver 4.0 for SQL Server". So I would use "-Dversion=4.0" in the mvn install. – George Armhold Apr 22 '12 at 18:26
  • 2
    I ran into an issue: "no POM in this directory" The solution to that can be found here: http://stackoverflow.com/questions/16348459/maven-3-0-5-command-error-the-goal-you-specified-requires-a-project-to-execute – Alan B. Dee Mar 28 '14 at 16:19
  • 4
    sourceforge jtds is your answer – Junchen Liu Sep 23 '14 at 12:30
  • 1
    You can download the jar here: http://www.java2s.com/Code/Jar/s/Downloadsqljdbc420jar.htm – Ismail Yavuz Jul 28 '15 at 14:37
  • 1
    Would downloading and adding it to the local maven repo not lead to a tight coupling with the development machine you are building your app on? I mean if the same project is tried to be build on a different machine, it would not find the jar in the local repo. Wouldn't it be good to add it to the project so that its available on VCS? – Obaid Maroof Dec 01 '15 at 10:25
  • Check this as well, https://stackoverflow.com/questions/30207842/add-external-library-jar-to-spring-boot-jar-internal-lib – Buminda Apr 06 '16 at 23:23
74

Answer for the "new" and "cool" Microsoft.

Yay, SQL Server driver now under MIT license on

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency>

Answer for the "old" Microsoft:

For my use-case (integration testing) it was sufficient to use a system scope for the JDBC driver's dependency as such:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>3.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/sqljdbc4.jar</systemPath>
    <optional>true</optional>
</dependency>

That way, I could put the JDBC driver into local version control. No need to have each developer manually set stuff up in their own repositories.

I took inspiration from this answer to another Stack Overflow question and I've also blogged about it here.

Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • If it's possible, I think it is better to upload the JAR on the local maven repository (Nexus) – mcoolive Mar 03 '15 at 16:21
  • @mcoolive: It definitely is better, mostly. But sometimes, it's just easier (and still appropriate) to do a quick-and-dirty job. – Lukas Eder Mar 03 '15 at 17:45
  • This answer is outdated compared to Touzery's updated answer. – Blessed Geek Dec 07 '16 at 19:19
  • 1
    @BlessedGeek: Well, I updated my answer even before Touzery. In fact, Touzery's answer is more outdated, because it still refernces a library from sourceforge. *sourceforge*!! – Lukas Eder Dec 07 '16 at 20:37
  • Don't forget to exclude azure-keyvault dependencies for new opensourced driver for versions <6.1.4. 6.1.3 and down to 6.1.0.jre7 add ridiculous amount of transitive dependencies. gladly, they fixed it. – Mikhail Boyarsky Mar 05 '17 at 01:04
  • 1
    @MikhailFedorov: My solution worked for me. Feel free to add your own answer (or even a question / answer pair) that shows how to solve this, would be great for the community. – Lukas Eder Mar 05 '17 at 09:49
17

There is also an alternative: you could use the open-source jTDS driver for MS-SQL Server, which is compatible although not made by Microsoft. For that driver, there is a maven artifact that you can use:

http://jtds.sourceforge.net/

From http://mvnrepository.com/artifact/net.sourceforge.jtds/jtds :

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

UPDATE nov 2016, Microsoft now published its MSSQL JDBC driver on github and it's also available on maven now:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency>
Emmanuel Touzery
  • 9,008
  • 3
  • 65
  • 81
9

I believe you are looking for the Microsoft SQL Server JDBC driver: http://msdn.microsoft.com/en-us/sqlserver/aa937724

Olaf
  • 6,249
  • 1
  • 19
  • 37
  • Yes, this is the one. I would like to load it through maven, so this was not actually answer to my question. So, first one giving that will get my tick for his/her answer! – mico Aug 05 '11 at 07:27
  • 3
    Looks like there are some disagreements between Maven and Microsoft folks regarding licensing and redistribution of the JDBC driver: http://blogs.msdn.com/b/jdbcteam/archive/2010/03/02/microsoft-sql-server-jdbc-3-0-ctp-release-announcement.aspx – Olaf Aug 05 '11 at 17:39
3

Be careful with the answers above. sqljdbc4.jar is not distributed with under a public license which is why it is difficult to include it in a jar for runtime and distribution. See my answer below for more details and a much better solution. Your life will become much easier as mine did once I found this answer.

https://stackoverflow.com/a/30111956/3368958

Community
  • 1
  • 1
Nelda.techspiress
  • 643
  • 12
  • 32
2
<dependency>
  <groupId>com.hynnet</groupId>
  <artifactId>sqljdbc4-chs</artifactId>
  <version>4.0.2206.100</version>
</dependency>

This worked for me(if you use maven)

https://search.maven.org/artifact/com.hynnet/sqljdbc4-chs/4.0.2206.100/jar

Akhil
  • 69
  • 1
  • 5
2

Even after installing the sqlserver jar, my maven was trying to fetch the dependecy from maven repository. I then, provided my pom the repository of my local machine and it works fine after that...might be of help for someone.

    <repository>
        <id>local</id>
        <name>local</name>
        <url>file://C:/Users/mywindows/.m2/repository</url>
    </repository>
Ashish
  • 3,572
  • 3
  • 18
  • 25
1

It looks like Microsoft has published some their drivers to maven central:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.1.0.jre8</version>
</dependency>
andrew-g-za
  • 967
  • 8
  • 13