MrSir
MrSir

Reputation: 596

Issue connecting spring-boot to azure sql server

I have a spring-boot project in which i try to connect to an azure sql database i just created. First time i tried it i was using sqljdbc4 under com.microsoft.sqlserver groupId, everytime i tried to launch it i had security errors, googling around i found out it's fixed by using 6+ versions. Then i installed it and set the dependency

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>slqjdbc6</artifactId>
        <version>6.2.1</version>
    </dependency>

Now i don't have that error anymore but instead i have org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.microsoft.sqlserver.jdbc.SQLServerDriver

Dependencies

<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-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>slqjdbc6</artifactId>
        <version>6.2.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

application.properties

spring.h2.console.enabled=true
spring.h2.console.path=/console
spring.datasource.platform=h2

#  production profile
spring.datasource.url=jdbc:sqlserver://spring-boot-intro.database.windows.net:1433;database=spring-boot-intro;user=fabio@spring-boot-intro;password=*my password*;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
spring.datasource.username=fabio
spring.datasource.password=*my password*
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

#  crud
spring.jpa.hibernate.ddl-auto=create-drop

slqjdbc6 under .m2 repository

C:\Users\*user*\.m2\repository\com\microsoft\sqlserver\slqjdbc6\maven-metadata-local.xml
C:\Users\*user*\.m2\repository\com\microsoft\sqlserver\slqjdbc6\6\_remote.repositories
C:\Users\*user*\.m2\repository\com\microsoft\sqlserver\slqjdbc6\6\slqjdbc6-6.2.1.jar
C:\Users\*user*\.m2\repository\com\microsoft\sqlserver\slqjdbc6\6\slqjdbc6-6.2.1.pom

Upvotes: 0

Views: 2792

Answers (3)

Jason DeMorrow
Jason DeMorrow

Reputation: 579

Try changing

spring.datasource.platform=h2

to

spring.datasource.platform=mssql

Upvotes: 0

Peter Pan
Peter Pan

Reputation: 24138

Try the two ways for resolving your issue.

  1. As @Ben said, please refer to the section Using the JDBC Driver with Maven Central of the document Download Microsoft JDBC Driver for SQL Server to add the mssql-jdbc driver dependency into the pom.xml file and install it via Maven Central.

  2. Or you can first download the jar file of MS SQL Server JDBC driver from the link of the above document. Then to run the command mvn install:install-file -Dfile=mssql-jdbc-6.2.1.jre8.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=mssqljdbc6 -Dversion=6.2.1 to install the maven dependency manually to local maven path and add the dependency content below to pom.xml.

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssqljdbc6</artifactId>
        <version>6.2.1</version>
    </dependency>
    

Hope it helps.

Upvotes: 0

Ben M
Ben M

Reputation: 1892

To where did you install that driver? The SQL Server drivers are not available in public Maven Repositories, so unless you have a private maven repository to which you've added that dependency, including it in your pom.xml will not result in the jdbc driver being available at runtime.

If you do not have a private / corporate maven repository to add the JDBC driver to, I would suggest adding the jar file within your source code and using a filesystem repository to reference that file.

You can refer to this answer for additional details on how to create such a repository Maven: add a dependency to a jar by relative path

Upvotes: 1

Related Questions