0

I have a local SQL Server instance. If I try to connect with Management Studio (SQL Server authentication, with username and password) everything works fine.

However, when I try to create a connection from a Spring Boot application I get some errors.

This is the code for the Datasource (dbUsername and dbpassword are correct)

@Bean
public DataSource getDataSource() throws SQLServerException {
    SQLServerDataSource ds = new SQLServerDataSource();
    ds.setUser(dbUsername);
    ds.setPassword(dbPassword);
    ds.setServerName("localhost");
    ds.setPortNumber(1433);
    ds.setDatabaseName("mydb");
    return ds;
}

When I try retrieve a connection with

Connection connection = dataSource.getConnection();

I get this error:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '"sa"'.

If I add ds.setIntegratedSecurity(true); I have a different error:

com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication.

This is the JDBC driver that I'm using:

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

Any suggestions?

3
  • 1
    Why are you even using SQLServerDataSource? In a web application it is better to use a data source that provides connection pooling. Spring Boot out of the box uses HikariCP for that, where you can define the datasource entirely in the config, without having to define a bean like this. Commented Jan 22, 2021 at 11:23
  • Hi @MarkRotteveel thank you for your comment; could you give me a link where I can find an example? Thanks a lot Commented Jan 22, 2021 at 11:26
  • 1
    You could start with Spring Boot Features: Working with SQL Databases Commented Jan 22, 2021 at 11:40

1 Answer 1

1

Personally, I encourage you to connect your DB by setting the configuration in the application.yaml file. It is in src/resources/application.yaml

  1. Create an application.yaml file (if it does not exist already).
  2. Copy this content:
spring:
  datasource:
    URL: jdbc:sqlserver://localhost:1433/mydb?reconnect=true
    username: username
    password: password
  1. Replace the username and password with yours.
  2. Delete the bean you've created.
  3. Good luck!
Sign up to request clarification or add additional context in comments.

4 Comments

Given the OP is using SQL Server, they would also need to replace the url.
URLs for SQL server start with jdbc:sqlserver: and are governed by the syntax documented on Building the Connection URL
@MarkRotteveel Thanks again! I looked at the docs of MSSQL :)
I solve using the application.properties file as suggested; I add these 3 entries spring.datasource.url, spring.datasource.username,spring.datasource.password and spring created a dataSource bean for me.

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.