netbeans没有为jython项目找到zxJDBC驱动程序

时间:2017-12-29 18:26:28

标签: python sql netbeans jython

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

see the snapshot here

并继续收到错误消息:

  

zxJDBC.DatabaseError:找不到驱动程序[com.microsoft.sqlserver.jdbc.SQLServerDriver]

这是我输入的代码:

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

1 个答案:

答案 0 :(得分:1)

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

输入Jython interpeter:

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()