Question

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?

Was it helpful?

Solution

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

OTHER TIPS

After struggling with this for a day, I finally found a solution. Don't bother with zxJDBC, Class.forName, DriverManager, etc - simply instantiate the driver directly:

import os
import sys
from java.util import Properties

# add the jar to your classpath, then import it
sys.path.append('/tmp/postgresql-8.4-701.jdbc4.jar')
import org.postgresql.Driver as Driver

props = Properties()
props.put('user', 'u')
props.put('password', 'p')

conn = Driver().connect('jdbc:postgresql://127.0.0.1', props)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top