使用Python使用jaydebeapi连接MYSQL DB

时间:2020-08-21 18:08:57

标签: python mysql jpype jaydebeapi

我正在尝试从Python提示符下使用JayDeBeApi连接到MySQL数据库。下面是我正在使用的代码段:

"**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)**

但是,我遇到了以下错误: “追踪(最近一次致电过去): 文件“”,第1行,位于 连接中的文件“ /。local / lib / python2.7 / site-packages / jaydebeapi / init .py”,第412行 jconn = _jdbc_connect(jclassname,url,driver_args,jar,libs) _jdbc_connect_jpype中的第221行的文件“ /。local / lib / python2.7 / site-packages / jaydebeapi / init .py” jpype.JClass(jclassname) 新建中的文件“ /。local / lib / python2.7 / site-packages / jpype / _jclass.py”,第130行 返回_JClassNew(args [0],** kwargs) _JClassNew中的文件“ /。local / lib / python2.7 / site-packages / jpype / _jclass.py”,第228行 javaClass = _jpype.PyJPClass(arg) jpype._jclass.NoClassDefFoundError:java.lang.NoClassDefFoundError:com / mysql / jdbc / Driver “

您能帮我了解我在哪里缺少什么,这里需要纠正什么吗? 预先感谢。

1 个答案:

答案 0 :(得分:0)

我在您指定的类路径上找不到正确的mysql驱动程序JAR。因此,您将得到所看到的ClassNotFoundException。尝试指定确切的路径。