Home > Software engineering >  Unable to connect to SQL Server database due to login failed error
Unable to connect to SQL Server database due to login failed error

Time:09-29

I have made a simple java project in which I am attempting to connect to an SQL Server 2019 (Developer Edition) database. However, when I try to do so, I get a login authentication error.

This is my code for the project:

package javafxapplication12;

import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

/**
 *
 * @author param
 */
public class FXMLDocumentController implements Initializable {
    Connection con;
    @FXML
    private Label label;
    
    @FXML
    private void handleButtonAction(ActionEvent event)  {
        label.setText("Hello World!");
         try{
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
     con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Financials;user=dbo;password=;Trusted_Connection=False;MultipleActiveResultSets=True");
 
     System.out.println("Connected to database !");
 
   }
   catch(Exception sqle) {
      System.out.println("Sql Exception :" sqle.getMessage());
      label.setText("Failed");
   }
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
    
}

However, when I compile this file, I am always getting this error:

Sql Exception :Login failed for user 'dbo'. ClientConnectionId:053ffe3f-aa4b-4c6b-86ee-df080cd91cf6

After reading for some time on Stack, I tried changing the hostname from localhost to myLaptopName, but I am still getting the same error, which leads me to believe that I am going wrong somewhere fundamentally.

Further, as suggested by some other users, I enabled SQL Server and Windows Authentication mode in Server Security settings, but even this didn't help resolve the error.

I am using JDK 1.8 with Netbeans 8.2 and mssql-jdbc-9.4.0.jre8.jar connector to connect to a MS SQL SERVER 2O19 database.

Also, I wanted to add that when I used this query SELECT HOST_NAME() in SSMS, I got the result myLaptopName. This is why I tried replacing localhost with myLaptopName.

Additional Information:

User name: dbo

Password: (no password)

myLaptopName refers to "LAPTOP-UQQOO5F7"

Database details:

Database

SSMS Login Screen:

enter image description here

Update: I tried to change the database name in the link to something different, just to check if that is causing any errors. Inspite of purposefully entering a wrong DB name (eg. FinAANCNAials), I am getting the same error !

CodePudding user response:

From the Screenshots, what I understood is that You are using Windows authentication to connect to the DB from SSMS, but you're using the SQL authentication connection string in the JDBC Code. There are 2 possible solutions

  1. Change the exiting connection string to Windows authentication.

All You've to do is to remove the username and password fields and provide integrated security as True in the existing connection string. Like this

jdbc:sqlserver://localhost:1433;databaseName=Financials;integratedSecurity=true
  1. Create a new SQL Authentication User and provide the credentials in the connection string.

You can create a new SQL user with the required roles in the system and replace the credentials in the existing connection string

Please refer to the following articles for more details

https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15

  • Related