0

I'm trying to connect to a MMSQL Database through python in a Jupyter environment with the addition of a JDBC Driver. Although everything should be set up in a correct manner the Error Message "TypeError: Class com.microsoft.sqlserver.jdbc.SQLServerDriver is not found" keeps reappearing everytime I run this code. The Paths should be set up corectly, the JAVA_HOME Variable aswell and the packages should work on Mac(M1). A friend of mine tested it on her windows notebook (also in a jupyter notebook file) and it worked for her without running into this error.

  • JDBC Driver: mssql-jdbc-12.8.1.jre11.jar
  • JDK version: "21.0.5"
  • Mac version: MacOS Sonoma (Version 14.5)
  • Python version: 3.11.5 Please let me know if there is any additional information that could be helpful!
import os import jpype import jaydebeapi import pandas as pd # Pfad zur JAR-Datei jdbc_driver_path = 'path/to/jar/file' if not os.path.isfile(jdbc_driver_path): print(f"JAR-Datei wurde nicht gefunden unter: {jdbc_driver_path}") else: print(f"JAR-Datei ist vorhanden: {jdbc_driver_path}") # Start JVM mit der JAR-Datei im Klassenpfad if not jpype.isJVMStarted(): jpype.startJVM(jpype.getDefaultJVMPath(), f"-Djava.class.path={jdbc_driver_path}") # Verbindungsparameter server = 'dwh...' database = 'db_name' username = 'username' password = 'password' # JDBC-URL jdbc_url = f'jdbc:sqlserver://{server};databaseName={database};encrypt=true;trustServerCertificate=true' # Verbindung zur Datenbank herstellen try: conn = jaydebeapi.connect( 'com.microsoft.sqlserver.jdbc.SQLServerDriver', jdbc_url, [username, password], jars=jdbc_driver_path ) print("Verbindung erfolgreich hergestellt") # Cursor erstellen cursor = conn.cursor() # Tabelle für Abfrage table_name = 'Facts_Monthly_Sales_Quota' # SQL-Abfrage zum Abrufen aller Daten der Tabelle query = f"SELECT * FROM {table_name}" # Abfrage ausführen und Ergebnisse in eine Liste laden cursor.execute(query) data = cursor.fetchall() columns = [desc[0] for desc in cursor.description] # DataFrame erstellen df = pd.DataFrame(data, columns=columns) # DataFrame anzeigen print(df) # Verbindung und Cursor schließen cursor.close() conn.close() except Exception as e: import traceback print("Fehler beim Herstellen der Verbindung:") traceback.print_exc() # Zeigt den vollständigen Stacktrace an 

Any help would be greatly appreciated!

I tried to set up a database connection to access the data, it should connect but it keeps running into the same error. Complete Output

1
  • 1
    Do not use images for text information. Add the text to the question with code block format. Also, the error is class not found, so probably the classpath is not correct. Commented Nov 10, 2024 at 13:33

1 Answer 1

0

The issue was not related to an incorrect class path or similar issues. I still don't know what the exact issue was or why the solution worked but I just refreshed the browser tab of the jupyter notebook root directory, that fixed my issue. Restarting the kernel multiple times did not work.

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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.