12

I am getting an exception while running my spring boot application, it say unable to create bean of datasource and caused of this exception is that it unable to detect my DriverClass for SQLServer.

Here is my application.properties file:

spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=dbname
spring.datasource.username=name
spring.datasource.password=****

I have downloaded it sqljdbc connector jar and put it in the lib folder and give its reference in my pom.xml file.

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<groupId>com.myproject</groupId>
<artifactId>myproject-notifications</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>netpace-notifications</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1.1</version>

        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/sqljdbc4-2.0.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

I have tried using these tags:

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

But it is giving error in my pom.xml file so that why I have to use the other approach. I am new to spring and would be very thankful if somebody let me know what I am doing wrong here. Thanks in advance.

2
  • can you post what exception message your getting. Commented Nov 22, 2017 at 8:07
  • What's the error???? Commented Nov 22, 2017 at 9:24

4 Answers 4

21

You are trying to load the wrong driver. The driver with name com.microsoft.jdbc.sqlserver.SQLServerDriver is from the very old SQL Server 2000 JDBC driver.

In the SQL Server 2005 JDBC driver, Microsoft changed this to com.microsoft.sqlserver.jdbc.SQLServerDriver (note the switch of order between sqlserver and jdbc.

Side note: you are using an old version of the driver. Microsoft has open sourced the SQL Server JDBC driver and it is available on maven as:

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

See https://github.com/Microsoft/mssql-jdbc for actual latest versions.

Sign up to request clarification or add additional context in comments.

Comments

0

Is the jar present on your system? You are forcing maven to get the dependency from the system and if it is not on that path maven will fail. You can replace the tag with these :

<dependency>
    <groupId>com.microsoft</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>3.0</version>
</dependency>

Source: https://mvnrepository.com/artifact/com.microsoft/sqljdbc4/3.0

2 Comments

I was not giving correct package name it was 'com.microsoft.sqlserver.jdbc.SQLServerDriver' instead of 'com.microsoft.jdbc.sqlserver.SQLServerDriver'.
That is a very old driver.
-1

In my case there was an extra space in the driver's name, after removing the space from the driver's name, the application started working fine.

driver=com.microsoft.sqlserver.jdbc.SQLServerDriver

Comments

-3

MAKE IDEA version 2020 TO 2019.3 solution this question

1 Comment

The original issue was with missing appropriate dependency. How the IDE upgrade will help in this case? I think OP was mentioning incorrect name of dependency that's why he was facing the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.