0
votes

Unable to connect SQL server using java.

Using SQL server management studio, i am able to connect with MFA account.But using src i am not able to connect.

public class Data {

public static void main(String[] args) throws SQLException {

     SQLServerDataSource ds = new SQLServerDataSource();
        ds.setServerName("demo.database.windows.net"); // Replace with your server name
        ds.setDatabaseName("demo"); // Replace with your database name
        ds.setUser("********");
        ds.setPassword("******");
     //   ds.setAuthentication("ActiveDirectoryMSI");

        Connection connection = ds.getConnection();
        java.sql.Statement stmt = connection.createStatement();

        try ( 

                ResultSet rs = stmt.executeQuery("SELECT * from customer;")) {
            if (rs.next()) {
                System.out.println("You have successfully logged on as: " + rs.getString(1));
            }
        }

}

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Cannot open server "demo.com" requested by the login. The login failed. ClientConnectionId:c40bc308-440c-4b01-9e11-fcbef63de19e at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217) at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:251) at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:81) at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:3080) at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2361) at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:44) at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2347) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1794) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1405) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1069) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:905) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:452) at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:637) at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(SQLServerDataSource.java:55) at database.Data.main(Data.java:24)

1

1 Answers

0
votes

Try doing this, change URL to a default URL Azure lets you pick from. EXPLANATION: MFA uses two level authentication. When any application is being registered for MFA, a CLIENT_SECRET parameter is provided. Azure requires the CLIENT_SECRET parameter for authentication. Hence the redirect URL method is used. It indicates to Azure that a native app is being registered and authentication is completed.