我无法从我的 Jython 程序连接到数据库。纯 Java 程序可以连接,我可以从 Jython 连接到 db,但只能使用 JDBC-ODBC 桥:“sun.jdbc.odbc.JdbcOdbcDriver”。如果我使用本机 JDBC 驱动程序,我的程序将因“找不到驱动程序”异常而失败。
代码:
import sys
from com.ziclix.python.sql import zxJDBC
connection1 = zxJDBC.connect('jdbc:odbc:test_odbc', 'postgres', 'postgres', 'sun.jdbc.odbc.JdbcOdbcDriver')
print "JDBC:ODBC connection set"
connection2 = zxJDBC.connect('jdbc:postgresql://127.0.0.1/test?stringtype=unspecified', 'postgres', 'postgres', 'org.postgresql.Driver')
print "JDBC native connection set"
输出:
C:\tools\pyscripts\scripts\db_examples>jython --version
Jython 2.5b1 (trunk:5903:5905, Jan 9 2009, 16:01:29)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_11
C:\tools\pyscripts\scripts\db_examples>jython pg_test.py
JDBC:ODBC connection set
Traceback (most recent call last):
File "pg_test.py", line 6, in <module>
connection2 = zxJDBC.connect('jdbc:postgresql://127.0.0.1/test?stringtype=un
specified', 'postgres', 'postgres', 'org.postgresql.Driver')
zxJDBC.DatabaseError: driver [org.postgresql.Driver] not found
我认为我的 CLASSPATH 设置正确,而本机 Java 程序可以使用本机驱动程序连接到该数据库。我发现所有 JDBC 驱动程序在 cachedir\packages 中都有 .pkc 文件。
我应该设置什么来获得数据库连接?