[英]Jython connect to mysql, Driver not found error
我正在尝试将Jython与mysql连接。 我下载了“ zxJDBC.jar”,“ mm.mysql-2.0.4-bin.jar”和“ mysql-connector-java-5.1.20-bin.jar”,并将它们的路径设置为CLASSPATH。
在我的Jython脚本中,
$from com.ziclix.python.sql import zxJDBC
$from org.gjt.mm.mysql import Driver
通过了。
但当
$conn = zxJDBC.connect("jdbc:mysql://localhost/automobile2", "root", "nihaonlp", "org.gjt.mm.mysql.Driver")
口译员告诉我
$zxJDBC.DatabaseError: driver [org.gjt.mm.mysql.Driver] not found
如何解决?
H
在这里看看:
http://glasblog.1durch0.de/?p=846
基本上,此技巧将使用ClassLoader加载您的类,以便您可以从jython中使用它
http://www.jython.org/jythonbook/zh/1.0/appendixB.html#using-the-classpath-steve-langer
我建议对此稍作修改:
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
我是来这里寻找相同错误的解决方案的。 问题是我在Eclipse Luna和pydev + jython中运行。 从我在其他地方读到的内容,IDE忽略了类路径环境变量,由于缺乏可移植性,因此它被认为是不好的做法。
无论如何,对于初学者来说eclipse / pydev相当繁重,我花了一些时间来找出解决方案,我想分享一下。
因此,在安装驱动程序之后,该驱动程序可以在任何位置运行,但当前Windows版本没有安装msi安装程序,该安装程序会将其放在Program Files(x86)\\ Mysql下。
在Eclipse中,转到“窗口”->“首选项”->“ PyDev”->“解释器”->“ Jython解释器”。
在此处,找到“库”面板(通常默认情况下处于打开状态),然后选择“ New Jar”按钮。 浏览找到已安装驱动程序的jar,然后选择它。 那应该将jar添加到列表中,然后在运行时找到驱动程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.