在Jython或Sikuli中返回zxJDBC错误?

时间:2014-07-01 14:39:10

标签: jdbc jython sikuli

我在返回zxJDBC错误时遇到了问题(具体来说,由于网络问题,我与MySQL的连接似乎丢失了,这导致我的程序崩溃/挂起),但老实说我不知道在Jython中返回.jar错误很多。我的大部分编程经验仅限于BASH& Perl过去,在Jython当前的冒险之前。 我要做的是返回所有zxJDBC错误,所以我可以识别&写一下try / except处理那些我可以处理的问题。

相关代码如下:

##########################################################
# from http://forum.java.sun.com/thread.jspa?threadID=300557
#
# Author: SG Langer Jan 2007 translated the above Java to this
#       Jython class
# Purpose: Allow runtime additions of new Class/jars either from
#       local files or URL
######################################################
class classPathHacker(object):
import java.lang.reflect.Method
import java.io.File
import java.net.URL
import java.net.URLClassLoader
def addFile(self, s):
    f = self.java.io.File(s)
    u = f.toURL()
    a = self.addURL(u)
    return a
def addURL(self, u):
    sysloader = self.java.lang.ClassLoader.getSystemClassLoader()
    sysclass = self.java.net.URLClassLoader
    method = sysclass.getDeclaredMethod("addURL", [self.java.net.URL])
    a = method.setAccessible(1)
    jar_a = jarray.array([u], self.java.lang.Object)
    b = method.invoke(sysloader, [u])
    return u

tmp = classPathHacker()
tmp.addFile("/opt/sikuli/libs/mysql-connector-java-5.1.29-bin.jar")
from com.ziclix.python.sql import zxJDBC
db = zxJDBC.connect("jdbc:SERVER/DATABASE", "USER", "PASSWORD", "com.mysql.jdbc.Driver", CHARSET='iso_1')
b = db.cursor()
b.execute("SOME_SQL_CODE_GOES_HERE")
for row in b:
    data = str(row)
    <DO IRRELEVANT THINGS WITH SQL RETURN>
b.close()
db.close()

我如何构造代码以捕获错误/异常?

我怀疑对于知道足够Java以将其翻译为Jython的人来说,有一些有价值的信息here

1 个答案:

答案 0 :(得分:0)

缺少的是try&amp;除了电话

基于以上所述,如果需要,可以调出特定的异常,但是否则可以返回所有异常:

##########################################################
# from http://forum.java.sun.com/thread.jspa?threadID=300557
#
# Author: SG Langer Jan 2007 translated the above Java to this
#       Jython class
# Purpose: Allow runtime additions of new Class/jars either from
#       local files or URL
######################################################
class classPathHacker(object):
import java.lang.reflect.Method
import java.io.File
import java.net.URL
import java.net.URLClassLoader
def addFile(self, s):
    f = self.java.io.File(s)
    u = f.toURL()
    a = self.addURL(u)
    return a
def addURL(self, u):
    sysloader = self.java.lang.ClassLoader.getSystemClassLoader()
    sysclass = self.java.net.URLClassLoader
    method = sysclass.getDeclaredMethod("addURL", [self.java.net.URL])
    a = method.setAccessible(1)
    jar_a = jarray.array([u], self.java.lang.Object)
    b = method.invoke(sysloader, [u])
    return u

tmp = classPathHacker()
tmp.addFile("/opt/sikuli/libs/mysql-connector-java-5.1.29-bin.jar")
from com.ziclix.python.sql import zxJDBC
db = zxJDBC.connect("jdbc:SERVER/DATABASE", "USER", "PASSWORD", "com.mysql.jdbc.Driver", CHARSET='iso_1')
try:
    b = db.cursor()
    b.execute("SOME_SQL_CODE_GOES_HERE")
    for row in b:
        data = str(row)
        <DO IRRELEVANT THINGS WITH SQL RETURN>
    b.close()
    db.close()
except:
    print("%s - [ERROR] %s \n" % (datetime.datetime.fromtimestamp(time.time()).strftime('%m-%d-%Y 5H:%M:%S'), sys.exc_info()[1]))

这会导致打印错误,看起来像“07-03-2014 12:10:39 - [ERROR]光标已关闭”,可以从那里进一步调试。

根据Jython Exception Handling & Debugging的官方网页回答。

PS - 很抱歉回答我自己的问题 - 不确定这样做是不好的juju;但由于我找不到另一个有类似问题的页面,我认为其他人可能会欣赏这些信息。