How do I connect SikuliX IDE to MySQL using zxJDBC

Asked by Peter Spierenburg

In the "Can I do X or Y or Z in Sikuli?" there is a bit about connecting sikuli to a database.

Anyway, my goal is to make the following script run:

    from com.ziclix.python.sql import zxJDBC
    db = zxJDBC.connect("jdbc:mysql//localhost/core", "xxx", "xxx", "com.mysql.jdbc.Driver", CHARSET='iso_1')

I've copied the mysql-connector-java-3.1.14-bin.jar file into sikuli's libs directory.

I've added an entry to the classpath in the runIDE.cmd script

I've tried adding the classpath entry inside my sikuli script

In all cases, I get:

[error] zxJDBC.DatabaseError ( driver [com.mysql.jdbc.Driver] not found )

What do I need to do in order to make this work?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Peter Spierenburg
Solved:
Last query:
Last reply:
Revision history for this message
Peter Spierenburg (peter-spierenburg) said :
#3

Here is the solution:

import jarray

class classPathHacker(object):
    """Original Author: SG Langer Jan 2007, conversion from Java to Jython
    Updated version (supports Jython 2.5.2) From http://glasblog.1durch0.de/?p=846

    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

    def addFile(self, s):
        """Purpose: If adding a file/jar call this first
        with s = path_to_jar"""
        # make a URL out of 's'
        f = self.java.io.File(s)
        u = f.toURL()
        a = self.addURL(u)
        return a

    def addURL(self, u):
        """Purpose: Call this with u= URL for
        the new Class/jar to be loaded"""
        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("C:\Program Files\Sikuli\libs\mysql-connector-java-5.1.26-bin.jar")

from com.ziclix.python.sql import zxJDBC
db = zxJDBC.connect("jdbc:mysql://localhost/core", "xxx", "xxx", "com.mysql.jdbc.Driver", CHARSET='iso_1')

Revision history for this message
RaiMan (raimund-hocke) said :
#4

great finding.

Sorry, I did not realize, that you are working with the IDE (though it is obvious in your question ;-)

SikuliX already has internal features, to add something to Java class path and or Jython sys.path at runtime.
I will make this feature publicly available in both API levels.

The problem with the JDBC driver, when it is set outside via class path environment is that it then comes before the Sikuli jar. This then hides stuff on sys.path and leads to the known problems.
So adding it later inside the script is the right solution.

Thanks again.

Revision history for this message
Kevin Ton (kevinton43) said :
#5

Thank you @Peter Spierenburg (peter-spierenburg). I am using sqlite3 instead of mysql but it still works like a charm.