0

I'm having trouble returning zxJDBC errors (in specific, my connection to the MySQL appears to drop due to network issues and this is causing my program to crash/hang), but will be honest that I don't know a great deal returning .jar errors in Jython. Most of my programming experience has been limited to BASH & Perl in the past, prior to this current adventure in Jython. What I am looking to do is return all zxJDBC errors, so I can identify & write try/except handling around those that I can deal with.

Relevant code is as follows:

##########################################################
# 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()

How would I structure the code to capture errors/exceptions?

I suspect there is some valuable information here for someone who knows enough Java to translate it to Jython.

Community
  • 1
  • 1
Keiron
  • 117
  • 1
  • 8

1 Answers1

0

What is missing are the try & except calls

Based on the above, specific exceptions can be called out if so desired, but otherwise all exceptions can be returned with:

##########################################################
# 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]))

This would cause an error to print looking something like "07-03-2014 12:10:39 - [ERROR] cursor is closed", which can be further debugged from there.

Answer based on the official webpage for Jython Exception Handling & Debugging.

PS - Sorry to answer my own question - not sure if it's bad juju to do SO; but since I couldn't find another page with a similar problem, I thought others might appreciate the info.

Keiron
  • 117
  • 1
  • 8