1

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 !

15
  • you should try with SELECT SERVERPROPERTY('ServerName') Commented Sep 28, 2021 at 6:10
  • 1
    and :Login failed for user error comes when your credentials are incorrect. so please verify that also Commented Sep 28, 2021 at 6:12
  • I tried that now and it is returning LAPTOP-UQQOO5F7 (which I had written as laptop name in my question). I tried replacing local host with this, but it still gives the same error. Commented Sep 28, 2021 at 6:12
  • The issue and answer lie there in the question itself, You're giving a user name but not a password, and without the password, the user is not able to authenticate. So Please provide a valid password or change the authentication method to Windows Commented Sep 28, 2021 at 6:23
  • Thanks @JayasuryaSatheesh for your suggestions. But, there is no password for the server. That is why I have left the field blank. Should I add password="" to my code ? Commented Sep 28, 2021 at 6:24

1 Answer 1

1

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://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. However, when I changed the connection string to what you have specified, I am getting this error: Sql Exception :This driver is not configured for integrated authentication. ClientConnectionId:170bd732-501a-4260-aa39-99cb0176a68b. Does this mean that my driver is improper for my server version ? Once again, thank you for taking out the time to answer my question !
@ParameshSriram Looks like this one is already answered here stackoverflow.com/questions/6087819/…
I tried copying the .dll files like they had suggested, and am still getting the same error. To find out if my driver was causing issues, I tried this before the first try-catch block: try { java.sql.DatabaseMetaData metaData = con.getMetaData(); System.out.println("Driver version:" + metaData.getDriverVersion()); } catch(Exception ae) { System.out.println(ae); } This returns a null pointer exception. Am I doing anything wrong with the drivers ?
Did you tried Googling the exact error again
Yesssss, I finally solved it by enabling SA login, and using the URL you provided. Idk how, but it worked !!!!! Thank you so much for your help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.