Reputation: 17
I'm trying to connect to a DB via the zxJDBC package for jython in netbeans IDE. I already specified the path to the zxJDBC.jar and the driver:
and keep receiving the error message:
zxJDBC.DatabaseError: driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] not found
This is the code that I'm typing:
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,))
The python platform I'm using is jython 2.5.1
Upvotes: 1
Views: 549
Reputation: 21
Probably is related to your CLASSPATH values. Jython needs to be related to the location of your JDBC drivers. In my case using Jython 2.7.0 in Linux, I review inside the interpeter, what are the CLASSPATH locations that Jython search by default:
Type into the 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']
>>>
The way that I found to resolve this problem, is call the driver's directory with the files, to the sys.path.
Example:
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()
Upvotes: 1