Python > Connection with JDBC to Oracle service name (jaydebeapi)

Yuriy Brovko picture Yuriy Brovko · Sep 19, 2016 · Viewed 10k times · Source

This sample code is used to connect in Python to Oracle SID.

import jpype
import jaydebeapi
jHome = jpype.getDefaultJVMPath()
jpype.startJVM(jHome, '-Djava.class.path=/path/to/ojdbc6.jar')
conn = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:user/password@DB_HOST_IP:1521:DB_NAME')

How can we connect to Oracle Service Name?

Answer

PKey picture PKey · Sep 19, 2016

Regarding your connection string, you could use TNS syntax (read on, here), as opposed to host:port:sid syntax you're using now. In that case you would describe SERVICE_NAME inside CONNECT_DATA, as follows:

   jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','[MYUSER]/[MYPASS]@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=[MYHOST])(PORT=1521))(CONNECT_DATA=(SERVER=dedicated) (SERVICE_NAME=[MYSERVICENAME])))')

By the way - you could also use cx_Oracle to connect to oracle - no java hassle. (just a suggestion)