tags:

views:

1540

answers:

3

I cannot connect to database from my Jython program. Pure Java programs can connect, and I can connect to db from Jython but only using JDBC-ODBC bridge: "sun.jdbc.odbc.JdbcOdbcDriver". If I use native JDBC driver my program fails with "driver not found" exception.

Code:

import sys
from com.ziclix.python.sql import zxJDBC

connection1 = zxJDBC.connect('jdbc:odbc:test_odbc', 'postgres', 'postgres', 'sun.jdbc.odbc.JdbcOdbcDriver')
print "JDBC:ODBC connection set"
connection2 = zxJDBC.connect('jdbc:postgresql://127.0.0.1/test?stringtype=unspecified', 'postgres', 'postgres', 'org.postgresql.Driver')
print "JDBC native connection set"

Output:

C:\tools\pyscripts\scripts\db_examples>jython --version
Jython 2.5b1 (trunk:5903:5905, Jan 9 2009, 16:01:29)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_11

C:\tools\pyscripts\scripts\db_examples>jython pg_test.py
JDBC:ODBC connection set
Traceback (most recent call last):
  File "pg_test.py", line 6, in <module>
    connection2 = zxJDBC.connect('jdbc:postgresql://127.0.0.1/test?stringtype=un
specified', 'postgres', 'postgres', 'org.postgresql.Driver')
zxJDBC.DatabaseError: driver [org.postgresql.Driver] not found

I think that my CLASSPATH is set properly while native Java programs can connect to this database using native driver. I have found that all JDBC drivers have .pkc files in cachedir\packages.

What should I set to get database connection?

+5  A: 

I will answer myself:

There was bug in Jython 2.5b1: Jython has problems to dynamically loading classes when installed on the boot classpath

I was able to run my program if I invoked it with --verify flag.

Bug disappeared in Jython 2.5b3

Michał Niklas
One Up for learning !
Blauohr
A: 

I had the same problem and could not use the --verify flag (jython complained about unknown switch). The problem disappeared magically as soon as I configured my OS X Leopard Java to use the 1.6 virtual machine instead of 1.5.

Jaanus