我正在尝试设置一种方法来在 GraphDB 预配置的 JDBC 表上执行 SQL 查询。我使用 SQuirrel SQL Client 等 SQL 引擎让它工作,但到目前为止我在 Python 中还没有成功。我尝试使用 python 包 jaydebeapi,但收到错误消息,指出找不到我的类 GraphDB localhost。
有人有使用这个 python 包的经验吗 - 特别是与 Ontotext GraphDB 或替代方案结合使用,并且可以在正确的方向上提供一些提示?
import jaydebeapi
# JDBC connection details
jdbc_url = "jdbc:graphdb:url=http://localhost:7200"
driver_class = "com.ontotext.graphdb.jdbc.remote.Driver"
driver_jar = "C:/Users/franken/Desktop/squirrelSQL/plugins/graphdb-jdbc-remote-10.0.0.jar" # Replace with the path to your GraphDB JDBC driver jar file
alias_name = "GraphDB localhost"
# SQL query to execute
sql_query = "SELECT * FROM GRAPHDB.IMAGINGBACKUPS.SOFTWARE"
# Establish connection
with jaydebeapi.connect(jclassname=alias_name, url=jdbc_url, driver_args=None, jars=driver_jar, libs=None) as conn:
with conn.cursor() as curs:
curs.execute(sql_query)
curs.fetchall()
我收到的错误:
(jdbc-py3.12) PS C:\Users\franken\jdbc\jdbc> python usesql.py
Traceback (most recent call last):
File "C:\Users\franken\jdbc\jdbc\usesql.py", line 13, in <module>
with jaydebeapi.connect(jclassname=alias_name, url=jdbc_url, driver_args=None, jars=driver_jar, libs=None) as conn:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\franken\jdbc\.venv\Lib\site-packages\jaydebeapi\__init__.py", line 412, in connect
jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\franken\jdbc\.venv\Lib\site-packages\jaydebeapi\__init__.py", line 221, in _jdbc_connect_jpype
jpype.JClass(jclassname)
File "C:\Users\franken\jdbc\.venv\Lib\site-packages\jpype\_jclass.py", line 99, in __new__
return _jpype._getClass(jc)
^^^^^^^^^^^^^^^^^^^^
TypeError: Class GraphDB localhost is not found
注意,当我将 jclassname 更改为 driver_class 时,我收到类似但不完全相同的错误 'java.lang.java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory' 错误。
(jdbc-py3.12) PS C:\Users\franken\jdbc\jdbc> python usesql.py
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Traceback (most recent call last):
File "ClassLoader.java", line 521, in java.lang.ClassLoader.loadClass
java.lang.java.lang.ClassNotFoundException: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "org.jpype.JPypeContext.java", line -1, in org.jpype.JPypeContext.callMethod
Exception: Java Exception
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\franken\jdbc\jdbc\usesql.py", line 13, in <module>
with jaydebeapi.connect(jclassname=driver_class, url=jdbc_url, driver_args=None, jars=driver_jar, libs=None) as conn:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\franken\jdbc\.venv\Lib\site-packages\jaydebeapi\__init__.py", line 412, in connect
jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\franken\jdbc\.venv\Lib\site-packages\jaydebeapi\__init__.py", line 230, in _jdbc_connect_jpype
return jpype.java.sql.DriverManager.getConnection(url, *dargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
java.lang.java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory