1

I'd like to connect to my SQL Server DB using zxJDBC, but I can't figure out which driver to use... My function looks like so:

def sqlServerConnect():
    conn = 'jdbc:sqlserver://MYDB'
    username = "username"
    password = "password"
    driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"

    try:
        conn = zxJDBC.connect(jdbc_url, username, password, driver)
        print "Connection successful"
    except zxJDBC.DatabaseError, e:
        print "Connection failed:", e

The error message reads as follows:

Connection failed: driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] not found
  • The JAR file for the JDBC driver needs to be available via the CLASSPATH when running the Jython script. See [this answer](http://stackoverflow.com/a/33445687/2144390) for examples. – Gord Thompson Dec 06 '16 at 15:32

1 Answers1

2

I found a solution, which is to use Jython's -J switch to give the JVM (Java Virtual Machine) a -cp classpath argument with the location of the JDBC jar file, e.g.,

jython -J-cp sqljdbc4.jar myProgram.py 
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418