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 !

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • you should try with SELECT SERVERPROPERTY('ServerName') – Ketan Kotak Sep 28 '21 at 06:10
  • 1
    and :Login failed for user error comes when your credentials are incorrect. so please verify that also – Ketan Kotak Sep 28 '21 at 06: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. – Paramesh Sriram Sep 28 '21 at 06: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 – Jayasurya Satheesh Sep 28 '21 at 06: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 ? – Paramesh Sriram Sep 28 '21 at 06:24
  • user dbo looks very-very strange. I guess you can try user sa with password (in case you have enabled mixed authentificationin SQL Server ) – Sergey Sep 28 '21 at 06:27
  • @Sergey, I changed the username to both `sa` and `param` now, (which is what I have logged in as) and that failed as well. Also, I confirmed that the password is blank by checking the login credentials which I use for logging into SSMS. Still, no luck :( – Paramesh Sriram Sep 28 '21 at 06:33
  • Seems, you are connecting to SQL Server using Windows-authentification As far as I remember, in this case you need to set in the connection string the parameter IntegratedSecurity=true and provide only the username – Sergey Sep 28 '21 at 06:34
  • @Sergey, I modified the URL to `con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Financials;user=param;password=;Trusted_Connection=False;IntegratedSecurity=true");` . I am now getting this error: `Sql Exception :This driver is not configured for integrated authentication. ClientConnectionId:fdeeea79-0bfe-4ef5-9e43-cd39acfac32f ` I have also added some images to show my credentials. – Paramesh Sriram Sep 28 '21 at 06:43
  • @ParameshSriram Try to use `Trusted_Connection=True` without `user` and `password`. – Zhorov Sep 28 '21 at 06:45
  • @Zhorov, I modified the URL to `con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Financials;Trusted_Connection=True");` . I am getting this error: `Login failed for user ''. ClientConnectionId:c86e83e0-c855-4f63-9e94-4e47e6e67f21`. – Paramesh Sriram Sep 28 '21 at 06:48
  • you might not have enable sa user. you can do it from security tab – Ketan Kotak Sep 28 '21 at 07:18
  • @KetanKotak, This did the trick for me, thank you for your answer ! – Paramesh Sriram Sep 28 '21 at 08:08
  • this is basically unrelated to fx - next time test/fix your database access in isolation before going UI :) – kleopatra Sep 28 '21 at 08:41
  • that's the basic mistake happens when fresh installation of sql – Ketan Kotak Sep 28 '21 at 09:58

1 Answers1

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

Jayasurya Satheesh
  • 7,826
  • 3
  • 22
  • 39
  • 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 ! – Paramesh Sriram Sep 28 '21 at 07:09
  • @ParameshSriram Looks like this one is already answered here https://stackoverflow.com/questions/6087819/jdbc-sqlserverexception-this-driver-is-not-configured-for-integrated-authentic – Jayasurya Satheesh Sep 28 '21 at 07:13
  • 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 ? – Paramesh Sriram Sep 28 '21 at 07:45
  • Did you tried Googling the exact error again – Jayasurya Satheesh Sep 28 '21 at 07:50
  • 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 :) – Paramesh Sriram Sep 28 '21 at 08:07