0

I have this conection to MySql in python with JayDeBeApi, using JDBC:

def data_JDBC(db_name, table, db, user, pwd):
    
    db = db.lower()
    db_name = db_name.lower()
    print(db)
    if db == 'postgresql':
        sql_str = f"select * from {db}.{table}"
        host='ec2-18-191-149-107.us-east-2.compute.amazonaws.com'
        port='5432'
        user=user
        pwd = pwd
        driver_name = 'org.postgresql.Driver'
        driver_path = 'path/to/postgresql-42.2.12.jar'        
    elif db == 'mysql':
        sql_str = f"select * from {table}"
        host='localhost'
        port='3306'
        user=user
        pwd = ""
        driver_name = 'com.mysql.cj.jdbc.Driver'
        driver_path = 'path/to/mysql-connector-java-8.0.20.jar'
        print(driver_path)
    connection_string=f'jdbc:{db}://'+ host+':'+ port +'/'+ db_name+'?useSSL=false&&serverTimezone=UTC&useLegacyDatetimeCode=false'
    if jpype.isJVMStarted() and not jpype.isThreadAttachedToJVM():
        jpype.attachThreadToJVM()
        jpype.java.lang.Thread.currentThread().setContextClassLoader(jpype.java.lang.ClassLoader.getSystemClassLoader())
    conn = jaydebeapi.connect(driver_name, connection_string,[user, pwd], jars=driver_path)
    frame = pd.read_sql(sql_str, conn);
    pd.set_option('display.expand_frame_repr', False)
    conn.close()
    return frame

This function returns a Pandas DataFrame, i have the jar files in the same directory. PostgreSQL works fine, but when i select for MySQL i get this error:

java.lang.RuntimeExceptionPyRaisable      Traceback (most recent call last)
<ipython-input-426-311d4ecabc53> in <module>
----> 1 dfMysql=data_JDBC(db_name='fuentedatos', table='tests', db='MySql', user='root', pwd='')
      2 dfMysql

...

java.lang.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class com.mysql.cj.jdbc.Driver not found

This is how i execute the function:

data_JDBC(db_name='fuentedatos', table='tests', db='MySql', user='root', pwd='')
3
  • did you check in Google com.mysql.cj.jdbc.Driver not found ? Probably you have to install some Java Class to connect with MySQL. Commented Jul 6, 2020 at 22:55
  • Is path/to/mysql-connector-java-8.0.20.jar a valid path and does the driver exist? Commented Jul 7, 2020 at 8:18
  • Yes it is a valid path, i'm sure of it. I try with 2 jar versions, 8.0.20.jar and 8.0.18.jar. None of them worked. I downloaded them from here dev.mysql.com/downloads/connector/j Commented Jul 7, 2020 at 14:20

2 Answers 2

1

For that particular error case there are two potential error causes.

1- Usually in the Java world the java.lang.RuntimeException: Class driverClassName not found exception is raised when we hadn't add the jar file's path to the CLASSPATH environment variable and that's what was exactly attempted to be explained here

2- (the most likely one) It seems like Jaydebeapi doesn't support the establishment of multiple connections for different data sources at the same time (github issue).

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

Comments

0

I encountered this issue because I accidentally used 2 containers at the same time, plase make sure only one container is active to open the local directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.