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()
DriverManager.registerDriver(driver)
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 (rset.next()):
print rset.getString(1)
stmt.close()
if __name__ == "__main__":
un = 'scott'
pw = 'tiger'
sid = 'orcl'
host = 'localhost'
port = '1521'
conn = connect(un, pw, sid, host, port)
doStuff(conn)
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 AMDid 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?
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