Oracle connectivity from Jython

Whenever automating web tests, you need to verify not only that the correct objects are appearing in the browser, but that the correct content is being retrieved from the database. This is not that easy when writing Selenium scripts in Selenese (the HTML table based language). It is however easy when writing Selenium RC scripts in whatever language you happen to be using, in my case it is Jython.

Inspired by this post, here is how you can access Oracle from within Jython. If you need to get different information, then you just add another method.

from oracle.jdbc.driver import OracleDriver
from java.sql import DriverManager
 
class oracle(object):
    def __init__(self, un, pw, sid, host, port):
        driver = OracleDriver()
        DriverManager.registerDriver(driver)
        connection_string = "jdbc:oracle:thin:@%s:%s:%s" % (host, port, sid)
        self.connection = DriverManager.getConnection(connection_string, un, pw)
 
    def getPermissionCode(self, uid):
        stmt = self.connection.createStatement()
        rset = stmt.executeQuery("select a bunch of stuff that isn't relevant for the example")
        permissions = []
        while (rset.next()):
            permissions.append(rset.getString("ROLE_ID"))
        stmt.close()
        return permissions
 
if __name__ == "__main__":
    db = oracle("user", "password", "sid", "host", "1521")
    codes = db.getPermissionCode("ADAM-JNH-CA-FS-0005")
    print codes

Post a Comment

Your email is never published nor shared. Required fields are marked *