Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load driver class: com.microsoft.jdbc.sqlserver.SQLServerDriver

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.

like image 754
Farrukh Ahmed Avatar asked Nov 22 '17 07:11

Farrukh Ahmed


1 Answers

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.

like image 176
Mark Rotteveel Avatar answered Oct 18 '22 08:10

Mark Rotteveel