如何使用 JayDeBeApi 连接到 Postgres?它没有找到 org.postgresql.Driver

How can I connect to Postgres using JayDeBeApi? It doesn't find the org.postgresql.Driver

我必须连接到 DB2 以提取一些数据并创建 Tableau 数据提取,为此我想使用 JayDeBeApi,为了测试它,我尝试将自己连接到一个小型 Postgres 数据库,但我无法与它建立正确的连接,因为它总是抛出以下错误:

jpype._jexception.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class org.postgresql.Driver not found

我目前的实现是这样的

import jaydebeapi as jdbc

sql = 'Select * From world.city'
postgresql_class = 'org.postgresql.Driver'
postgresql_jdbc_path = 'path/to/postgresql-42.2.4.jar'
postgresql_url = 'jdbc:postgresql://host:port/database'
postgresql_user = 'user'
postgresql_pw = 'pass'

conn = jdbc.connect(postgresql_class, 
                    [postgresql_url, postgresql_user, postgresql_pw], 
                    postgresql_jdbc_path)

curs = conn.cursor()

curs.execute('SELECT * FROM csv_reports LIMIT 2')
curs.fetchall()
curs.close()
conn.close()

有谁知道 class 我必须指定什么才能完成这项工作?或者我必须如何指定 jdbc 路径,以便 JayDeBeApi 可以使用 class 或其他东西

根据 JayDeBeApi documentation

The first argument to connect is the name of the Java driver class. The second argument is a string with the JDBC connection URL. Third you can optionally supply a sequence consisting of user and password or alternatively a dictionary containing arguments that are internally passed as properties to the Java DriverManager.getConnection method. See the Javadoc of DriverManager class for details.

所以您不应该将连接 URL 与用户名和密码一起包含在列表中;它应该单独作为第二个参数:

jdbc.connect(postgresql_class, 
             postgresql_url, 
             [postgresql_user, postgresql_pw], 
             postgresql_jdbc_path)