Andrew Channels Dexter Pinion

Wherein I write some stuff that you may like to read. Or not, its up to you really.

July 19, 2005

Using the Oracle Thin JDBC Driver from Jython

I've got precisely one piece of code to write for my current project. Luckily for me I can write it in Jython. This means I need to learn it. Step 1 is connecting to our Oracle database. Because the server the code will work on already has the JDBC drivers installed I can just call them from my Jython code. Looking around the web I couldn't find any sample code though, other than this example in proper Java. So here's my version, using Jython 2.1;

from oracle.jdbc.driver import OracleDriver
from java.sql import DriverManager

def connect(un, pw, sid, host, port):
     driver = OracleDriver()
     connection = "jdbc:oracle:thin:@%s:%s:%s" % (host, port, sid)
     conn = DriverManager.getConnection(connection, un, pw)
     return conn

def doStuff(conn):
     stmt = conn.createStatement()
     rset = stmt.executeQuery("SELECT banner FROM sys.v_$version")
     while (
         print rset.getString(1)

if __name__ == "__main__":
     un = 'scott'
     pw = 'tiger'
     sid = 'orcl'
     host = 'localhost'
     port = '1521'
     conn = connect(un, pw, sid, host, port)

Posted by Andy Todd at July 19, 2005 11:32 AM


Jython 2.1? That's just *so* last week...

Posted by: Simon Brunning on July 20, 2005 01:10 AM

Did this work for you? I have the oracle drivers installed by when I do from oracle.jdbc.driver import OracleDriver, it says there's no driver 'oracle'. Am I missing a step?

Posted by: Andrew on September 8, 2005 01:35 AM

I just added the JDBC Driver jar (ojdbc14.jar) to my CLASSPATH and it works. Thanks.

Posted by: John Mudd on February 28, 2006 12:53 AM