在从命令行调用的Jython脚本中使用UCanAccess

时间:2015-10-30 14:29:28

标签: eclipse command-line jython ucanaccess

操作系统:Windows7,Jython2.7.0FR(“最终版本”)

尝试在Jython中使用Gord Thompson的解决方案:Manipulating an Access database from Java without ODBC

我可以让Ucanaccess模块​​在Eclipse中运行,但是当我尝试从CLI运行时则不行。

# -*- coding: utf-8 -*-
import java, sys
jars_dir = 'D:\\sysadmin\\Resources\\Java jar files\\'
sys.path.append( jars_dir + 'commons-lang-2.6.jar' )
sys.path.append( jars_dir + 'commons-logging-1.2.jar' )
sys.path.append( jars_dir + 'hsqldb.jar' )
sys.path.append( jars_dir + 'jackcess-2.1.2.jar' )
sys.path.append( jars_dir + 'ucanaccess-3.0.2.jar' )
import net.ucanaccess.jdbc.UcanaccessDriver
import net.ucanaccess
print( '# appear to have imported UcanaccessDriver' )
conn = java.sql.DriverManager.getConnection( 'jdbc:ucanaccess://D:/TESTING.mdb' )
print( '# conn OK...') 

当我在Eclipse中运行它时,打印出“#conn OK”。 当我从CLI运行它时,它打印出“#似乎已导入...”,但随后出现错误:

enter image description here

(注意,此输出已复制到Eclipse:它确实是一个CLI运行)

任何人都知道为什么我会得到“没有合适的司机......”?注意我在getConnection中尝试了使用反斜杠而不是前向斜杠的各种排列......但无济于事。

顺便说一句,如果这是相关的,这是我用来运行该东西的.bat文件的摘录:

cd "%SOFTWARE_PROJECTS%\workspace\Jython scratchpad\src\jython_scratchpad_root"

REM this is probably irrelevant and doesn't seem to work with Jython2.7.0FR.  The jars are being loaded by sys.path.append, obviously
set CLASSPATH=.;"%SYSADMIN%\resources\java jar files/*"

D:\apps\jython2.7.0\bin\jython loading_test.py 

1 个答案:

答案 0 :(得分:2)

It appears that sys.path has more to do with the PATH environment variable than the CLASSPATH environment variable. The following two methods have been tested and do work with Jython 2.7.0 to run a script called "jyTest.py" from the Windows command line

Method 1: Set the CLASSPATH from a batch file (or shell script) before launching Jython

SET CLASSPATH=C:/Users/Public/Downloads/UCanAccess/ucanaccess-3.0.2.jar;C:/Users/Public/Downloads/UCanAccess/lib/commons-lang-2.6.jar;C:/Users/Public/Downloads/UCanAccess/lib/commons-logging-1.1.1.jar;C:/Users/Public/Downloads/UCanAccess/lib/hsqldb.jar;C:/Users/Public/Downloads/UCanAccess/lib/jackcess-2.1.2.jar;.  
c:\jython2.7.0\bin\jython jyTest.py

(For Linux et. al. that would be export CLASSPATH ..., and colon separators instead of semicolons.)

Method 2: Use -J-cp to set the CLASSPATH when invoking Jython

c:\jython2.7.0\bin\jython -J-cp C:/Users/Public/Downloads/UCanAccess/ucanaccess-3.0.2.jar;C:/Users/Public/Downloads/UCanAccess/lib/commons-lang-2.6.jar;C:/Users/Public/Downloads/UCanAccess/lib/commons-logging-1.1.1.jar;C:/Users/Public/Downloads/UCanAccess/lib/hsqldb.jar;C:/Users/Public/Downloads/UCanAccess/lib/jackcess-2.1.2.jar;. jyTest.py

(Again, Linux and friends would need the -cp entries to be separated by colons instead of semicolons.)