Setting up maven dependency for SQL Server - sql-server

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?

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>

Answer for the "new" and "cool" Microsoft.
Yay, SQL Server driver now under MIT license on
GitHub: https://github.com/Microsoft/mssql-jdbc
Maven Central: http://search.maven.org/#search%7Cga%7C1%7Cmssql-jdbc
<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.

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>

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

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

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>

<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

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>

Related

jdk17 application not able to connect to sybase

This application runs without any error with jdk8.
jdk version jdk17
jConnect dependency
<dependency>
<groupId>com.sybase.jconnect</groupId>
<artifactId>jconn4</artifactId>
<version>7.07-27307</version>
</dependency>
oracle dependency
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8_g</artifactId>
<version>12.2.0.1</version>
</dependency>
Error : Could not load JDBC driver class [com.sybase.jdbc4.jdbc.SybDriver]
I tried by upgrading jConnect jar to 16.039-27463 and ojdbc to ojdbc10 version 19.3 but of no use.
Did anyone face this before and how got it fixed.

spring boot configuration with MS SQL Server 2016

I am trying to connect to Microsoft SQL Server 2016 from spring boot application. SQL server is configured using windows authentication.
Following are the configuration
application.properties file
spring.datasource.url=jdbc:sqlserver://K877DTRV:1433;databaseName=testdb;integratedSecurity=true
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.initialize=true
pom.xml
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
java version is 1.8.spring boot version is 2.0.3.RELEASE
I am getting the below error while starting the tomcat
Cannot load driver class: com.microsoft.sqlserver.jdbc.SQLServerDriver
Should I do any additional configurations?
Note: I am using Windows Authentication.
Can anyone please help ?
Setting a version to the pom entry for mssql-jdbc worked like a charm
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version><!--$NO-MVN-MAN-VER$-->
</dependency>

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

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.
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.
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>
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.
* 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
Then Refresh/clean the project.
Thank you!
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
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.
just add
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
<scope>runtime</scope>
</dependency>
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.
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>
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
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>
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

Is there any SolrJ documentation available for connecting with Solr 4.0 or ahead

I'm unable to connect to my Solr instance on Tomcat from SolrJ. I've been through the documentation shown at "http://wiki.apache.org/solr/Solrj" , but its pretty outdated - primarily because it has no reference on Solr cores. I'm wondering if somebody could point me to the latest documentation OR advise on how to connect SolrJ with Solr4.0 or ahead - although my instance is running just 1 core for now.
Here's my connection string: "localhost:8080/solr-example/collection1/". Do you know which jars to add along with solrj? Thats where the trouble might be. For examples, the SolrJ wiki references a jar called commons-codec-1.3.jar which is not to be found anywhere, in the solr 4.0 zip file.
Calling Solr from Java with SolrJ
You can specify the Core directly in the URL.
HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/my_core");
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery(q);
solrQuery.setStart(start);
solrQuery.setRows(rows);
QueryResponse response = server.query(solrQuery);
HttpSolrServer is reusable for more queries.
The communication between the Solr Server and SolrJ happens via HTTP with a custom binary format. Can you Browse the Solr Web Admin at http://localhost:8080/solr? Depending on your installation, you might need to adjust the port (8080 is default on Tomcat, jetty uses 8983).
Also, did you deploy Solr with a generic name or did you include the Version? Than your URL would be http://localhost:8080/solr-4.2.1/my_core
Dependencies
These are the minimum dependencies you need for using SolrJ. Add these to your pom.xml, if you are using maven.
<dependency>
<artifactId>solr-solrj</artifactId>
<groupId>org.apache.solr</groupId>
<version>4.2.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
If you are not using maven, you need the following jars:
solr-solrj-4.2.0
zookeeper-3.4.5
commons-io-2.1
httpclient-4.2.3
httpcore-4.2.2
commons-codec-1.6
httpmime-4.2.3
wstx-asl-3.2.3
slf4j-simple-1.5.6
slf4j-api-1.7.2
commons-logging-1.1.1

Flyway Unable to instantiate jdbc driver

Just starting out with Flyway and Spring 3.0. So far, all I did was add the Flyway dependency and plugin to my pom.xml. Next, I tried running mvn flyway:status in the command line. However, it complains that it is unable to instantiate the jdbc driver (I'm using postgres).
Does anybody know what might be causing this? I'm using Springsource Tool Suite to develop my app. The postgres driver is located under WEB-INF/lib/postgresql-9.1-902.jdbc4.jar
Any help is greatly appreciated! Thanks!
For the Maven plugin to work you must:
Add this dependency to your project (or just the plugin):
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
and configure the plugin like this:
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://...</url>
<user>...</user>
<password>...</password>
</configuration>
</plugin>
You also have to provide the Postgresql jdbc drivers as a maven dependency:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-902.jdbc4</version>
</dependency>

Resources