0

I'm trying to connect to Azure SQL Database using the JDBC driver because ODBC is very slow. I need to use ActiveDirectoryIntegrated authentication.

Python code for connection below:

server = 'jdbc:sqlserver://someserver.database.windows.net:1433;database=someserver;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;Authentication=ActiveDirectoryIntegrated'
cnxn = jay.connect('com.microsoft.sqlserver.jdbc.SQLServerDriver', server,
                               jars=[r'C:\mssql-jdbc-9.4.0.jre8.jar',r'C:\msal4j-1.11.0.jar'])

I'm getting this error:

    Traceback (most recent call last):
  File "java.lang.ClassLoader.java", line -1, in java.lang.ClassLoader.loadClass
  File "sun.misc.Launcher$AppClassLoader.java", line -1, in sun.misc.Launcher$AppClassLoader.loadClass
  File "java.lang.ClassLoader.java", line -1, in java.lang.ClassLoader.loadClass
  File "java.net.URLClassLoader.java", line -1, in java.net.URLClassLoader.findClass
java.lang.ClassNotFoundException: java.lang.ClassNotFoundException: com.nimbusds.oauth2.sdk.auth.ClientAuthentication

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "org.jpype.JPypeContext.java", line -1, in org.jpype.JPypeContext.callMethod
  File "java.lang.reflect.Method.java", line -1, in java.lang.reflect.Method.invoke
  File "sun.reflect.DelegatingMethodAccessorImpl.java", line -1, in sun.reflect.DelegatingMethodAccessorImpl.invoke
  File "sun.reflect.NativeMethodAccessorImpl.java", line -1, in sun.reflect.NativeMethodAccessorImpl.invoke
  File "sun.reflect.NativeMethodAccessorImpl.java", line -2, in sun.reflect.NativeMethodAccessorImpl.invoke0
  File "java.sql.DriverManager.java", line -1, in java.sql.DriverManager.getConnection
  File "java.sql.DriverManager.java", line -1, in java.sql.DriverManager.getConnection
  File "SQLServerDriver.java", line 915, in com.microsoft.sqlserver.jdbc.SQLServerDriver.connect
  File "SQLServerConnection.java", line 1470, in com.microsoft.sqlserver.jdbc.SQLServerConnection.connect
  File "SQLServerConnection.java", line 2471, in com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal
  File "SQLServerConnection.java", line 2628, in com.microsoft.sqlserver.jdbc.SQLServerConnection.login
  File "SQLServerConnection.java", line 2978, in com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper
  File "SQLServerConnection.java", line 3488, in com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand
  File "IOBuffer.java", line 7417, in com.microsoft.sqlserver.jdbc.TDSCommand.execute
  File "SQLServerConnection.java", line 4227, in com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute
  File "SQLServerConnection.java", line 88, in com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000
  File "SQLServerConnection.java", line 4289, in com.microsoft.sqlserver.jdbc.SQLServerConnection.logon
  File "SQLServerConnection.java", line 5560, in com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon
  File "tdsparser.java", line 37, in com.microsoft.sqlserver.jdbc.TDSParser.parse
  File "tdsparser.java", line 125, in com.microsoft.sqlserver.jdbc.TDSParser.parse
  File "tdsparser.java", line 289, in com.microsoft.sqlserver.jdbc.TDSTokenHandler.onFedAuthInfo
  File "SQLServerConnection.java", line 4680, in com.microsoft.sqlserver.jdbc.SQLServerConnection.processFedAuthInfo
  File "SQLServerConnection.java", line 4724, in com.microsoft.sqlserver.jdbc.SQLServerConnection.onFedAuthInfo
  File "SQLServerConnection.java", line 4844, in com.microsoft.sqlserver.jdbc.SQLServerConnection.getFedAuthToken
  File "SQLServerConnection.java", line 4872, in com.microsoft.sqlserver.jdbc.SQLServerConnection.msalContextExists
  File "java.lang.Class.java", line -1, in java.lang.Class.forName
  File "java.lang.Class.java", line -2, in java.lang.Class.forName0
Exception: Java Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\AppData\Roaming\JetBrains\JetBrainsDS2021.3\scratches\scratch_1.py", line 11, in <module>
    trg_num_rows_campaign_m = get_query(db_conf,'select count(*) from srcSFDC.CampaignMember',
  File "C:\Users\Jupyter labs\func.py", line 30, in wrap
    result = func(*args, **kwargs)
  File "C:\Users\Jupyter labs\func.py", line 112, in get_query
    cnxn = jay.connect('com.microsoft.sqlserver.jdbc.SQLServerDriver', server,
  File "C:\Program Files\Python\Python39\lib\site-packages\jaydebeapi\__init__.py", line 412, in connect
    jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
  File "C:\Program Files\Python\Python39\lib\site-packages\jaydebeapi\__init__.py", line 230, in _jdbc_connect_jpype
    return jpype.java.sql.DriverManager.getConnection(url, *dargs)
java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: com/nimbusds/oauth2/sdk/auth/ClientAuthentication

I don't understand what is wrong and how can I fix it.

If I use this code:

cnxn = jay.connect('com.microsoft.sqlserver.jdbc.SQLServerDriver', server,
                           jars=r'C:\mssql-jdbc-9.4.0.jre8.jar'
                           ,libs=r'C:\mssql-jdbc_auth-9.4.0.x64.dll')

Driver is not seeing dll:

com.microsoft.sqlserver.jdbc.SQLServerException: com.microsoft.sqlserver.jdbc.SQLServerException: Failed to load both mssql-jdbc_auth-9.4.0.x64 and MSAL4J Java library for performing ActiveDirectoryIntegrated authentication. Please install one of them to proceed.

0 Answers0