简体   繁体   中英

Can not connect to MS SQL Server using JDBC driver SQLState 08001

I am trying to connect to a MS SQL Server database using Java but I keep running into the same error.

I am using MS SQL Server for developers and am maniging it through SSMS. When I installed it I set is so it would use my windows credentials as password.

The JDBC driver is added to my project as a Maven dependency.

From what I understand reading the Microsoft doc's I should be able to use the following connection string:

String connectionUrl ="jdbc:sqlserver://localhost;integratedSecurity=true";

However this does not seem to work for me as it returns SQLState 08001. After reading up on this state I understand that it has something to do with failing to make the connection.

I also tried to form my connection string using my windows login name, which results in the same:

String connectionUrl = "jdbc:sqlserver:localhost:1433;databaseName=DBNAME;user=John Doe;password=0123456789";

I also tried to create a login using T-SQL to use as credentials using the same string as the previous (only changing the username and password):

CREATE ROLE [USER]
GRANT SELECT, INSERT ON SCHEMA :: [dbo] TO USERNAME
CREATE LOGIN USERNAME_LOGIN WITH PASSWORD = '1234567890'
CREATE USER USER_USERNAME FROM LOGIN USERNAME_LOGIN
ALTER ROLE [USER] ADD MEMBER USER_USERNAME

I could really use some help to understand what I am doing wrong and what I can do to make it work. If you require me to check anything please let me know how as I am still pretty new to this.

Edit

As requested here is the info coming from getMessage():

No suitable driver found for jdbc:sqlserver:localhost:1433;databaseName=.....

I am guessing this would also be the moment to mention the Maven dependency I am importing:

<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
       <groupId>com.microsoft.sqlserver</groupId>
       <artifactId>mssql-jdbc</artifactId>
       <version>7.3.0.jre8-preview</version>
</dependency>

I think the question now becomes, do I have the wrong driver as a dependency for MS SQL Server 2017?

I turned out I did not had TCP/IP connections enabled for MS SQL Server.

To enable this in MS SQL Server 2017:

  • Start SQLServerManager14.msc from the windows application menu.
  • Go to SQL Server Network Configuration.
  • Go to Protocols for YOURINSTANCE.
  • Set TCP/IP to enabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM