简体   繁体   English

Jython连接到mysql,找不到驱动程序错误

[英]Jython connect to mysql, Driver not found error

I'm trying to connect Jython with mysql. 我正在尝试将Jython与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. 我下载了“ zxJDBC.jar”,“ mm.mysql-2.0.4-bin.jar”和“ mysql-connector-java-5.1.20-bin.jar”,并将它们的路径设置为CLASSPATH。

In my Jython script, both 在我的Jython脚本中,

$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 H

take a look here: 在这里看看:

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

Basically, this trick will load your class using ClassLoader so that you can use it from jython 基本上,此技巧将使用ClassLoader加载您的类,以便您可以从jython中使用它

http://www.jython.org/jythonbook/en/1.0/appendixB.html#using-the-classpath-steve-langer http://www.jython.org/jythonbook/zh/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

I came here looking for a solution for the same error. 我是来这里寻找相同错误的解决方案的。 The problem is that I am running within Eclipse Luna plus pydev + jython. 问题是我在Eclipse Luna和pydev + jython中运行。 From what I read elsewhere, classpath environment variable is ignored by IDEs, considered to be bad practice for lack of portability. 从我在其他地方读到的内容,IDE忽略了类路径环境变量,由于缺乏可移植性,因此它被认为是不好的做法。

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. 无论如何,对于初学者来说eclipse / pydev相当繁重,我花了一些时间来找出解决方案,我想分享一下。

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. 因此,在安装驱动程序之后,该驱动程序可以在任何位置运行,但当前Windows版本没有安装msi安装程序,该安装程序会将其放在Program Files(x86)\\ Mysql下。

In Eclipse, go to Window->Prefences->PyDev->Interpreters->Jython Interpreter. 在Eclipse中,转到“窗口”->“首选项”->“ PyDev”->“解释器”->“ Jython解释器”。

In here, look for the panel Libraries (usually open by default) and select New Jar button. 在此处,找到“库”面板(通常默认情况下处于打开状态),然后选择“ New Jar”按钮。 Browse to find the jar for the driver as installed and select it. 浏览找到已安装驱动程序的jar,然后选择它。 That should add the jar to the list and the driver will then be found at run-time. 那应该将jar添加到列表中,然后在运行时找到驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM