简体   繁体   中英

Connecting MYSQL DB using jaydebeapi using Python

I'm trying to connect to a MySQL DB from Python prompt and using JayDeBeApi. Below is the piece of code I'm using:

"**Python 2.7.16 (default, Oct 10 2019, 22:02:15) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import jpype
>>> import jaydebeapi
>>> import os
>>> jar = "/tmp/:/usr/share/java/"
>>> os.environ['JAVA_HOME'] ="/usr/lib/jvm/adoptopenjdk-8-hotspot-amd64"
>>> os.environ['CLASSPATH'] =jar
>>> args='-Djava.class.path=%s' % jar
>>> jvm_path = jpype.getDefaultJVMPath()
>>> jpype.startJVM(jvm_path, args)
/home/aarpan_roy/.local/lib/python2.7/site-packages/jpype/_core.py:218: UserWarning: 
-------------------------------------------------------------------------------
Deprecated: convertStrings was not specified when starting the JVM. The default
behavior in JPype will be False starting in JPype 0.8. The recommended setting
for new code is convertStrings=False.  The legacy value of True was assumed for
this session. If you are a user of an application that reported this warning,
please file a ticket with the developer.
-------------------------------------------------------------------------------

  """)
>>> database_host='<MySQL DB HOST>'
>>> database_user='<MySQL DB USER>'
>>> database_password='<MySQL DB PASSWORD>'
>>> database_port='<MySQL DB PORT>'
>>> database_db='<MySQL DB DATABASE>'
>>> jclassname = "com.mysql.jdbc.Driver"
>>> url = "jdbc:mysql://{host}:{port}/{database}".format(host=database_host, port=database_port, database=database_db)
>>> driver_args = [url, database_user, database_password]
>>> jars = ["/tmp/mysql-connector-java-5.1.45.jar","/usr/share/java/mysql.jar"]
>>> libs = None
>>> cnx = jaydebeapi.connect(jclassname, url, driver_args, jars=jars, libs=libs)**

But, I'm getting below error: "Traceback (most recent call last): File "", line 1, in File "<Home_PATH>/.local/lib/python2.7/site-packages/jaydebeapi/ init .py", line 412, in connect jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs) File "<HOME_PATH>/.local/lib/python2.7/site-packages/jaydebeapi/ init .py", line 221, in _jdbc_connect_jpype jpype.JClass(jclassname) File "<HOME_PATH>/.local/lib/python2.7/site-packages/jpype/_jclass.py", line 130, in new return _JClassNew(args[0], **kwargs) File "<HOME_PATH>/.local/lib/python2.7/site-packages/jpype/_jclass.py", line 228, in _JClassNew javaClass = _jpype.PyJPClass(arg) jpype._jclass.NoClassDefFoundError: java.lang.NoClassDefFoundError: com/mysql/jdbc/Driver "

Can you please help me understand where I'm missing something and what needs to be corrected here? Thanks in advance.

I cannot spot the proper mysql driver JAR on your specified classpath. Therefore you get the ClassNotFoundException you are seeing. Try to specify the exact path to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM