【问题标题】:Connecting MYSQL DB using jaydebeapi using Python使用 Python 使用 jaydebeapi 连接 MYSQL DB
【发布时间】:2020-12-11 03:27:24
【问题描述】:

我正在尝试从 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, jars, libs) _jdbc_connect_jpype 中的文件“/.local/lib/python2.7/site-packages/jaydebeapi/init.py”,第 221 行 jpype.JClass(jclassname) new 中的文件“/.local/lib/python2.7/site-packages/jpype/_jclass.py”,第 130 行 return _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 "

您能否帮助我了解我在哪里遗漏了什么以及这里需要纠正什么? 提前致谢。

【问题讨论】:

    标签: python mysql jpype jaydebeapi


    【解决方案1】:

    我无法在您指定的类路径上找到正确的 mysql 驱动程序 JAR。因此,您会看到您看到的 ClassNotFoundException。尝试指定它的确切路径。

    【讨论】:

      猜你喜欢
      • 2020-06-29
      • 1970-01-01
      • 2018-04-05
      • 2017-08-28
      • 2017-01-26
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      相关资源
      最近更新 更多