1

I'm trying to connect Jython with mysql. I downloaded "zxJDBC.jar", "mm.mysql-2.0.4-bin.jar" and "mysql-connector-java-5.1.20-bin.jar", and set their path to CLASSPATH.

In my Jython script, both

$from com.ziclix.python.sql import zxJDBC
$from org.gjt.mm.mysql import Driver

passed.

But when

$conn = zxJDBC.connect("jdbc:mysql://localhost/automobile2", "root", "nihaonlp", "org.gjt.mm.mysql.Driver")

The interpreter told me

$zxJDBC.DatabaseError: driver [org.gjt.mm.mysql.Driver] not found

How to fix it?

h

2 Answers 2

1

take a look here:

http://glasblog.1durch0.de/?p=846

Basically, this trick will load your class using ClassLoader so that you can use it from jython

http://www.jython.org/jythonbook/en/1.0/appendixB.html#using-the-classpath-steve-langer

I recommend modfiying the script slightly to this:

class classPathHacker:
    ##########################################################
    # from http://forum.java.sun.com/thread.jspa?threadID=300557
    #
    # Author: SG Langer Jan 2007 translated the above Java to this
    #       Jython class
    # Modified 2012 by Malte Vesper
    # Purpose: Allow runtime additions of new Class/jars either from
    #       local files or URL
    ######################################################
    import java.lang.reflect.Method
    import java.io.File
    import java.net.URL
    import java.net.URLClassLoader
    import jarray

    def addFile (self, path):
        #############################################
        # Purpose: If adding a file/jar call this first
        #       with path = path_to_jar
        #############################################

        return self.addURL (self.java.io.File (path).toUrl())

    def addURL (self, url):
        ##################################
        # Purpose: Call this with u= URL for
        #       the new Class/jar to be loaded
        #################################

        parameters = self.jarray.array([self.java.net.URL], self.java.lang.Class)
        sysloader =  self.java.lang.ClassLoader.getSystemClassLoader()
        sysclass = self.java.net.URLClassLoader
        method = sysclass.getDeclaredMethod("addURL", parameters)
        jar_a = self.jarray.array([url], self.java.lang.Object)
        method.invoke(sysloader, jar_a)
        return url
2
  • After 4 hours of frustration dealing with Jython and the JDBC driver I found Steve Langer's hack by way of ted's answer. It works like a charm.
    – Renklauf
    Nov 15, 2012 at 20:56
  • this should be unnecessary now that Jython supports amending the classpath over sys.path.append. Aug 19, 2015 at 15:33
0

I came here looking for a solution for the same error. The problem is that I am running within Eclipse Luna plus pydev + jython. From what I read elsewhere, classpath environment variable is ignored by IDEs, considered to be bad practice for lack of portability.

Anyway, eclipse/pydev is quite heavy for a beginner and it took me some time to figure out the solution and I would like to share it.

So, after installing the driver, which can go anywhere but current Windows versions have a msi installer which will put it under Program Files (x86)\Mysql.

In Eclipse, go to Window->Prefences->PyDev->Interpreters->Jython Interpreter.

In here, look for the panel Libraries (usually open by default) and select New Jar button. Browse to find the jar for the driver as installed and select it. That should add the jar to the list and the driver will then be found at run-time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.