Search code examples
pythondatabasejpype

python - java.lang.Exception: Class oracle.jdbc.driver.OracleDriver not found


Being a newbie to python, trying to write a python code to connect to the oracle database without using any Instant client. i'm using jaydebeapi and jpype as suggested in some other threads in this forum. After lot of hurdles, i now got stuck at this error. here is the code.

import jaydebeapi
import jpype
try:
    con = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver', ['windb19.ams.com', 'AA3112D1OS', 'advantage', 'C:\Tools\ojdbc8.jar'])
    cur = con.cursor()
    cur.execute('select * from r_sc_user_info')
except Exception as e:
    print e

and the error i'm receiving is as below

C:\Python27\python.exe C:/Project/Robot_Framework/SampleProject/CustomLibraries/DBLibrary.py
java.lang.Exception: Class oracle.jdbc.driver.OracleDriver not found

Process finished with exit code 0

Solution

  • As I couldn't modify anything in the Environment variables, as per the policy, I had to modify the code as below to make it work. I had to keep the ojdbc8.jar in the same path as that of this python file and add following lines of code.

    jar=os.getcwd()+'\ojdbc8.jar'
    args = '-Djava.class.path=%s' % jar
    
    jvm_path = jpype.getDefaultJVMPath()
    jpype.startJVM(jvm_path, args)
    
    try:
        con = jaydebeapi.connect("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@HOSTNAME",["USERID", "PASSWORD"], jar)