gpt4 book ai didi

python - netbeans 找不到 jython 项目的 zxJDBC 驱动程序

转载 作者:太空宇宙 更新时间:2023-11-03 14:19:41 25 4
gpt4 key购买 nike

我正在尝试通过 netbeans IDE 中 jython 的 zxJDBC 包连接到数据库。我已经指定了 zxJDBC.jar 和驱动程序的路径:

see the snapshot here

并不断收到错误消息:

zxJDBC.DatabaseError: driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] not found

这是我正在输入的代码:

from com.ziclix.python.sql import zxJDBC    
conn = zxJDBC.connect("jdbc:sqlserver://SERVERNAME;DatabaseName=DBNAME;","USER","PASSWORD","com.microsoft.sqlserver.jdbc.SQLServerDriver")
cursor = conn.cursor()
cursor.execute("select * from TABLE1")

for row in cursor:
print('row = %r' % (row,))

我使用的python平台是jython 2.5.1

最佳答案

可能与您的 CLASSPATH 值有关。Jython 需要与 JDBC 驱动程序的位置相关。在我在 Linux 中使用 Jython 2.7.0 的情况下,我在解释器中查看了 Jython 默认搜索的 CLASSPATH 位置:

在 Jython 解释器中输入:

Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_151
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/opt/jython/Lib', '__classpath__', '__pyclasspath__/', '/opt/jython/Lib/site-packages']
>>> sys.path.append("/opt/jython/jdbc/derby.jar")
>>> sys.path.append("/opt/jython/jdbc/derbyclient.jar")
>>> sys.path
['', '/opt/jython/Lib', '__classpath__', '__pyclasspath__/', '/opt/jython/Lib/site-packages', '/opt/jython/jdbc/derby.jar', '/opt/jython/jdbc/derbyclient.jar']
>>>

我发现解决此问题的方法是将驱动程序的目录与文件调用到 sys.path。

示例:

Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.8.0_151
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/opt/jython/Lib', '__classpath__', '__pyclasspath__/', '/opt/jython/Lib/site-packages']
>>> sys.path.append("/opt/jython/jdbc/derby.jar")
>>> sys.path.append("/opt/jython/jdbc/derbyclient.jar")
>>> sys.path
['', '/opt/jython/Lib', '__classpath__', '__pyclasspath__/', '/opt/jython/Lib/site-packages', '/opt/jython/jdbc/derby.jar', '/opt/jython/jdbc/derbyclient.jar']
>>> from com.ziclix.python.sql import zxJDBC
>>> d, u, p, v = "jdbc:derby://localhost:1527//opt/apache-derby/derbydata/derbytutor/mundialito", "APP", "APP", "org.apache.derby.jdbc.ClientDriver"
>>> db = zxJDBC.connect(d, u, p, v)
>>> c = db.cursor()
>>> c.execute("SELECT * FROM COUNTRY_FIFA_RANK")
>>> for a in c.fetchall():
... print a
...
(1, u'Germany', u'GER', datetime.date(2017, 6, 12), 1, 1, None, None, None)
(2, u'Brazil', u'BRA', datetime.date(2017, 6, 12), 2, 2, None, None, None)
(3, u'Portugal', u'POR', datetime.date(2017, 6, 12), 3, 3, None, None, None)
>>> c.close()
>>> db.close()

关于python - netbeans 找不到 jython 项目的 zxJDBC 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48026805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com