Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to microsoft sql server from spring jpa

Tags:

java

spring

jpa

I am using microsoft sql server,

If i open intellij, open new database connection in database tool window =>

enter image description here

and in advanced type i add: enter image description here

The connection works. However now i want to connect to this ms sql server with spring jpa. So what i am using is:

spring.datasource.url=jdbc:jtds:sqlserver://<host>:<port>;instance=<instance>;domain=<domain>;useNTLMv2=true
spring.datasource.username=<user>
spring.datasource.password=<password>
spring.datasource.driverClassName=net.sourceforge.jtds.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect

And it just says "login for user failed"

In my pom i am using:

<!-- jpa -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>${spring.jpa.version}</version>
</dependency>

<dependency>
    <groupId>jtds</groupId>
    <artifactId>jtds</artifactId>
    <version>1.2</version>
</dependency>

Why does it work with database tool window, but not with jpa?

Thanks for help!

like image 940
Johnyb Avatar asked Mar 05 '26 03:03

Johnyb


1 Answers

Your are mixing JDBC driver connecting with sprig data JPA connection, for JPA proper connection you can change the drive class name to

spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

Then the dialect can be

spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect

And finally add the proper url and remove JDBC driver relavent dependencies

spring.datasource.url=jdbc:sqlserver://localhost;databaseName=<dbname>

Remaining properties will be same as your, see both examples in here .

like image 189
Lunatic Avatar answered Mar 06 '26 16:03

Lunatic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!