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)
stmt = conn.createStatement()
rset = stmt.executeQuery("SELECT banner FROM sys.v_$version")
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