SOLVED

Login failed for user '<token-identified principal>' but works in Data Studio

Copper Contributor

Hi,

 

I am trying to use my AD account to connect to the Azure SQL using Java 8, JDBC Driver, and my accessToken.

 

When I use Data Studio using my AD Account, I can connect successfully to the Azure SQL DB.

 

But when I use my Java Program then it gives me this error:

Request processing failed; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '<token-identified principal>'

 

My code abstract:

 

SQLServerDataSource ds = new SQLServerDataSource();
        ds.setServerName("NAME.database.windows.net"); 
        ds.setDatabaseName("db-name"); 
        ds.setAccessToken(accessToken);
        ds.setEncrypt(true);
        ds.setTrustServerCertificate(true);
        try (Connection connection = ds.getConnection();
                Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT SUSER_SNAME()")) {
            if (rs.next()) {
                System.out.println("dbResults => You have successfully logged on as: " + rs.getString(1));
                res = rs.getString(1);
            }
        }


1 Reply
1 best response