python Jython classPathHacker加载JDBC

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Jython classPathHacker加载JDBC相关的知识,希望对你有一定的参考价值。

def loadJar(jarFile):
    '''load a jar at runtime using the system Classloader (needed for JDBC)

    adapted from http://forum.java.sun.com/thread.jspa?threadID=300557
    Author: Steve (SG) Langer Jan 2007 translated the above Java to Jython
    Reference: https://wiki.python.org/jython/JythonMonthly/Articles/January2007/3
    Author: seansummers@gmail.com simplified and updated for jython-2.5.3b3+

    >>> loadJar('jtds-1.3.1.jar')
    >>> from java import lang, sql
    >>> lang.Class.forName('net.sourceforge.jtds.jdbc.Driver')
    <type 'net.sourceforge.jtds.jdbc.Driver'>
    >>> sql.DriverManager.getDriver('jdbc:jtds://server')
    jTDS 1.3.1
    '''
    from java import io, net, lang
    u = io.File(jarFile).toURL() if type(jarFile) <> net.URL else jarFile
    m = net.URLClassLoader.getDeclaredMethod('addURL', [net.URL])
    m.accessible = 1
    m.invoke(lang.ClassLoader.getSystemClassLoader(), [u])

if __name__ == '__main__':
    import doctest
    doctest.testmod()

以上是关于python Jython classPathHacker加载JDBC的主要内容,如果未能解决你的问题,请参考以下文章

python (jython) 归档库

如何将 python 模块添加到 jython/lib?

使用 jython 运行 python

python JButton Jython示例!!!!

python Jython classPathHacker加载JDBC

Cpython和Jython的对比介绍