using com.mysql.jdbc.Driver (official JDBC driver from MySQL) in Sikuli script --- workaround

Asked by Dave Doodeman on 2012-07-06

********** workaround

see comment #6

----------------------------------------

I'm trying to make a connection in Sikuli script with a MySQL database.
(First (after of course creating the database, downloading and installing the driver....) set the classpath)

My code:

from com.ziclix.python.sql import zxJDBC
jdbc_url = 'jdbc:mysql://localhost:3306/BinckAutoTest'
username = 'root'
password = 'password'
MySQLDriver = 'com.mysql.jdbc.Driver'
urldb = zxJDBC.connect(jdbc_url, username, password, MySQLDriver)
#do something with it
..
..

I get in return:
zxJDBC.DatabaseError: driver [com.mysql.jdbc.Driver] not found

I followed the Jython documentation and this should work.
Does Sikuli handle tar files differently then normal Jython(does it, for example, ignore the classpath)?
If so, how do I make my driver known in Sikuli?

I'm new at the Java/Jython/Sikuli business so If there is something really simple that I couldn't have missed, please say it because I probably did.

Thanks

Dave

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Dave Doodeman
Solved:
2012-07-12
Last query:
2012-07-12
Last reply:
2012-07-12
RaiMan (raimund-hocke) said : #1

What system?
what version of Sikuli?

-- Does Sikuli handle tar files differently then normal Jython(does it, for example, ignore the class path)?

guess it is a typo: ... Sikuli handle tar files ...
With Java class path we are talking about .jar files

Where and how do you set the class path?

How do you run the script?

Dave Doodeman (killingman) said : #2

-system=windows
-version sikuli= 1.0rc3(r905)
-Typo = confirmed (indeed it is jar not tar)
-Classpath set with cmd:
set CLASSPATH=H:\Download\MySQL\mysql-connector-java-5.1.21\mysql-connector-java-5.1.21\mysql-connector-java-5.1.21-bin.jar;%CLASSPATH%
-srcipt run from sikuli workbench with "play slowly"-button(the yellow one)

RaiMan (raimund-hocke) said : #3

recommendation: use latest build r930/931 (see faq 1766), but has nothing to do with your problem.

Guess you are running the IDE from a command line using Sikuli-IDE.bat/Sikuli-IDE-w.bat

I confirm, that there is no chance, to modify the class path from externally, when using the Jython script run feature of Sikuli (IDE or from command line).

You might try it at the beginning of the script e.g.:

import java.lang.System as JS
cp = JS.getProperty("java.class.path")
print cp
cp += ":some.jar"
JS.setProperty("java.class.path", cp)
print JS.getProperty("java.class.path")

mind the colon as separator!

Or you run your scripts using an external Jython installation.

Dave Doodeman (killingman) said : #4

I used classpath update as suggested but it did not change the problem.
Because of a lack of time I just let it rest for a while.
When I get back to it, I'm thinking of just go and write java use the database from there and incorperate the Sikuli script as a jar. That should work.

Carter Zhang (carterzhang15) said : #5

I also encountered the error when I'd like to connect mysql in sikuli:
zxJDBC.DatabaseError: driver [com.mysql.jdbc.Driver] not found

I tried as you say and also it does not work.
Beause of the need of through sikuli(Jython) connecting the DB, so...
Looking forward to your further reply, thank you all.

RaiMan (raimund-hocke) said : #6

I made some tests with Sikuli and Jython 2.5.3 running them in there default configuration from command line

Whatever you do, you get the message:
zxJDBC.DatabaseError: driver [.....Driver] not found

when using the mysql driver
mysql-connector-java-5.1.21-bin.jar

The problem seems to be, that the driver is named < com.mysql.jdbc.Driver > and a com directory is in the Jython internal Lib directory, that comes before the __classpath__ reference in sys.path. So Jython tries to find the mysql.dbc.Driver in this internal Lib/com directory (where it is not ;-).

Running a sikuli script this way, works with a standalone Jython (need not be installed, just downloaded and unzipped):

-- a bash script
cp=<some-folder>//mysql-connector-java-5.1.21/mysql-connector-java-5.1.21-bin.jar
cp=$cp:<some-other-folder>/jython.jar
java -cp $cp org.python.util.jython $1.sikuli/$1.py

the <..folder> have to be adjusted to your situation.
This brings < com.mysql.jdbc.Driver > into the first place and it is found.

If you exchange the jython.jar by sikuli-script.jar it works too.

In any case you need an
from sikuli import *
at the beginning of your script, if you want to use Sikuli features.

--- the test script:
MySQLDriver = 'com.mysql.jdbc.Driver'
from com.ziclix.python.sql import zxJDBC
jdbc_url = 'jdbc:mysql://localhost:3306/BinckAutoTest'
username = 'root'
password = 'password'
urldb = zxJDBC.connect(jdbc_url, None, None, MySQLDriver)

--- the printout:
Traceback (most recent call last):
  File "jython.sikuli/jython.py", line 13, in <module>
    urldb = zxJDBC.connect(jdbc_url, None, None, MySQLDriver)
zxJDBC.DatabaseError: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. [SQLCode: 0], [SQLState: 08S01]

Carter Zhang (carterzhang15) said : #7

Hi RaiMan, I referred to what you said and succeeded in connecting to the MySQL server through Sikuli.
Thank you very much!

RaiMan (raimund-hocke) said : #8

confirmed by user

Dave Doodeman (killingman) said : #9

Raiman,

How do I use the bash script on a windows environment?

Dave

RaiMan (raimund-hocke) said : #10

Ok, you have to transform it to a Windows command file ;-)

something like that

set cp=<some-folder>/mysql-connector-java-5.1.21/mysql-connector-java-5.1.21-bin.jar
set cp=%cp%:<some-other-folder>/jython.jar
java -cp %cp% org.python.util.jython %1.sikuli/%1.py

and the / have to be \

I am not sure, wether on Windows the colon ( : ) in the class path must be a semicolon ( ; )

Dave Doodeman (killingman) said : #11

It's alive...
Ok, now I need to get some code done

Thanks RaiMan